+ Start a Discussion
Sankalita Chatterjee 19Sankalita Chatterjee 19 

REST to create multiple object - JSON Deserialization failed on token null

I have been trying to create CurrencyType records from APEX and since that doesn't support DML, have been using the REST endpoint and POST method. Everything work fine with single record but with multiple records, I am getting this error:
[ { "message": "Json Deserialization failed on token 'null' and has left off in the middle of parsing a row. Will go to end of row to begin parsing the next row", "errorCode": "INVALID_FIELD" } ]

I don't see how the code worked for single object, if it really were an Invalid Field.
I have seen couple of related posts but have been unable to fix my issue.

Here is my code:
public static void syncCurrencyTypeTableMultiple(){

 Http h = new Http(); 
HttpRequest req = new HttpRequest(); 

req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v41.0/sobjects/CurrencyType/'); 
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId()); 
req.setHeader('Content-Type', 'application/json'); 
req.setMethod('POST'); 

List<CurrencyType> currTypeList = new List<CurrencyType>(); 

for(sanmgdpack__PST_Currency__c curr: [Select sanmgdpack__ISO_Code__c, sanmgdpack__Conversion_Rate__c, sanmgdpack__Decimal_Places__c FROM sanmgdpack__PST_Currency__c]) { 
CurrencyType currTypeObj = new CurrencyType( IsoCode = curr.sanmgdpack__ISO_Code__c, ConversionRate = curr.sanmgdpack__Conversion_Rate__c, DecimalPlaces = Integer.valueOf(curr.sanmgdpack__Decimal_Places__c) ); currTypeList.add(currTypeObj); 
} 
String jsonStr = JSON.serializePretty(currTypeList); 
System.debug('json Str:' + jsonStr); 

//test //List<CurrencyType> deserList = (List<CurrencyType>)JSON.deserialize(jsonStr, List<CurrencyType>.class); 
//System.debug('deserlist::::' + deserList); //works fine 

req.setBody(jsonStr); 
System.debug(req); 
HttpResponse res = h.send(req); 
System.debug(res); 
}

 
NagendraNagendra (Salesforce Developers) 
Hi Chatterjee,

Hi Chatterjee,

You are, trying to insert multiple CurrencyType records in a single REST call?

If so, you need to use the REST Composite Batch API https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_composite_batch.htm that has some limitations. There is an excellent discussion on options in this blog posthttp://www.fishofprey.com/2017/01/

Thanks,
Nagendra