1

I am doing a rest call with the below endpoint

string endPoint = 'https://test.salesforce.com/services/data/v43.0/query?q=SELECT+Name+,+RecordTypeId+,+Code__c+from+Account';

I have the below class to map the above response

public class WrapperClass {

    public List<resultWrapper> records {get;set;} 

    public class resultWrapper{
        public string Name {get;set;}
        public string RecordTypeId {get;set;}
        public string Code__c {get;set;}   //// Here i am getting the error "Invalid character in identifier"
    }
}

Http h1=new Http();
HttpRequest req1=new HttpRequest();
req1.setHeader('Authorization','Bearer '+wResp.access_token);
req1.setHeader('Content-Type','application/json');
req1.setMethod('GET');
req1.setEndpoint(endPoint);
HttpResponse hresp1=h1.send(req1);
    WrapperClass wResp1=(WrapperClass) JSON.deserialize(hresp1.getBody(),WrapperClass.class);

JSON

{

    "totalSize": 2073227,
    "done": false,
    "nextRecordsUrl": "/services/data/v43.0/query/XXXXXXXXXXXXXXXX-2000",
    "records": [{
        "attributes": {
            "type": "Account",
            "url": "/services/data/v43.0/sobjects/Account/0012500000j2rfNAAQ"
        },
        "Name": "Test ytymnjkhkjhhuty0",
        "RecordTypeId": "012D0000000QoolIAC",
        "Code__c": "1234567890"
    }]
}

How to resolve this error. How to map Custom field to Json Variables.

6
  • 1
    Can you post code which you used to deserialise json? Also rename Code__c to Code and try.
    – Reshma
    Jan 17 '19 at 11:55
  • I have updated the code...If i change it to Code, it wont match the json variable and will return null.. Jan 17 '19 at 12:02
  • Share the sample JSOn which you are receiving. Jan 17 '19 at 12:04
  • I have updated it with JSon Jan 17 '19 at 12:09
  • Why are you using a wrapper class resultWrapper? Your records are just sObjects, so you can deserialize a List<My_Sobject__c>.
    – David Reed
    Jan 17 '19 at 12:14
2

You cannot have a variable with name ending with __c. They are reserved only for custom fields. You have to do a bit of String Manipulation from the receieved callout JSON.

public class WrapperClass {

        public List<resultWrapper> records {get;set;} 

        public class resultWrapper{
            public string Name {get;set;}
            public string RecordTypeId {get;set;}
            public string Code_Tempc{get;set;}   //// Here i am getting the error "Invalid character in identifier"
        }
    }

And your rest code:

HttpResponse hresp1=h1.send(req1);
WrapperClass wResp1=(WrapperClass) JSON.deserialize(hresp1.getBody().replaceAll('__c','Tempc'),WrapperClass.class);
1
  • 2
    Not reserved for only custom fields. Also part of namespace prefix scheme.
    – Adrian Larson
    Jan 17 '19 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.