I am making a callout from my org to the REST endpoint of another org in our business. This is using the standard query endpoint /services/data/v48.0/query?q=SELECT+Fields+FROM+Contact+WHERE+....

While I am accessing the Contact object, there are fields being returned that I need that doesn't exist in my own Contact object, therefore I can't just use List<Contact>.

For this, I created what I am calling a wrapper but probably using that term incorrectly here. This would give me the basic structure of the response, along with the records that I am trying to access.

The problem however is that the contact object on the other org has a custom field called Custom_Field__c. This is a valid field for their org, and I need the value for it in the response.

 * Contact Parent Structure Response
public class ContactWrapper {
    @AuraEnabled public Integer totalSize {get; set;}
    @AuraEnabled public Boolean done {get; set;}
    @AuraEnabled public List<ContactWrapperRecords> records {get;set;} 

 * Contact record
public class ContactWrapperRecords {
    @AuraEnabled public String Name {get; set;}
    @AuraEnabled public String Id {get; set;}
    @AuraEnabled public String Custom_Field__c {get; set;}
    @AuraEnabled public String Email {get; set;}

The problem I am getting when trying to define what ContactWrapperRecords looks like is that the field ends with __c, and this throws an error of Invalid character in identifier: Custom_Field__c.

Here is what my AuraEnabled APEX method looks like:

 * Get enterprise contact records
public static ContactWrapper getEnterpriseContacts(String contactIds) {

    // Define the case service
    HTTPRestService_EnterpriseCaseCreation caseService = new HTTPRestService_EnterpriseCaseCreation();

    // Make GET callout
    String ep = restBaseEndpoint + restQueryEndpoint;
    HttpResponse calloutResponse = caseService.makeGetCallout(namedCredentialEnterprise, ep, contactIds);
    ContactWrapper records = new ContactWrapper();

    // Check response status
    if (calloutResponse.getStatusCode() == 200) {
        records = (ContactWrapper) System.JSON.deserialize(calloutResponse.getBody(), ContactWrapper.class);

    // Return
    return records;

What would be the best way to access this custom field ending in __c that's part of the response?

I thought about being lazy and doing something like this: calloutResponse.getBody().replaceAll('__c',''). but I don't feel that's the correct way to handle this and will probably bite me later on.

Suggestions on an approach?

  • @identigral - Not quite as I followed the same pattern of turning my JSON response into the apex code I am currently using. Since the original response contains the field ending in __c, its using that same value in the definition it generates which is where I am running into the problem. A field can't end in __c, but since it is a custom field coming from another org, thats how it is being sent. – SBB Nov 2 '20 at 17:01
  • It won't work with typed deserialization unless you replace the identifier prior to deserializing. Take a look at 'JSON attribute is a reserved word or invalid identifier' paragraph in the referenced Q&A. – identigral Nov 2 '20 at 17:03
  • Okay, so it looks like they are doing what I was going to attempt : theJson.replace('"currency":','"currencyX":'),MyStuff.class);. Rather than do this per field though, its probably easier to handle it like I mentioned, just removing __c so I can omit those from the definition. Seemed a little hacky but I suppose it will do. – SBB Nov 2 '20 at 17:09