5

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
    Commented Jan 17, 2019 at 11:55
  • I have updated the code...If i change it to Code, it wont match the json variable and will return null.. Commented Jan 17, 2019 at 12:02
  • Share the sample JSOn which you are receiving. Commented Jan 17, 2019 at 12:04
  • I have updated it with JSon Commented Jan 17, 2019 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
    Commented Jan 17, 2019 at 12:14

2 Answers 2

6

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
  • 4
    Not reserved for only custom fields. Also part of namespace prefix scheme.
    – Adrian Larson
    Commented Jan 17, 2019 at 13:56
0

If you are using Custom field on an object and you are getting this error, then you must try _ (Single Underscore) instead of __ (Double Underscore).

public class resultWrapper{
    public string Name {get;set;}
    public string RecordTypeId {get;set;}
    public string Code_c {get;set;} 
}

I hope this resolves your error

1
  • 1
    This doesn't seem to add anything that the existing answer hasn't already covered.
    – Derek F
    Commented Oct 5, 2023 at 11:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .