+ Start a Discussion
Hemanth BakthavatsaluHemanth Bakthavatsalu 

Getting error while sending JSON object to update

From Salesforce I am trying  to make restful api call to a CPQ system.

1. I am able to make a login (POST request)
2. I am able to query the Account object & retrieve the id & fields on the Account object in the external system through (GET request)
3. Now I need to update the Account record with a value from Salesforce

Example: I have written the JSON Generator code to update Account name on the external system using the Id of the Account retrieved.


        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        
        JSONGenerator gen = JSON.createGenerator(true);
        gen.writeStartArray();
        gen.writeStartObject();
        gen.writeStringField('Id', '0ca0000f8pxds72r');
        gen.writeStringField('Name', 'TEST12345');
        gen.writeEndObject();
        gen.writeEndArray();
        String jsonOrders1 = gen.getAsString();
        System.debug('jsonOrders: ' + jsonOrders1 );

        req.setMethod('POST');        
        req.setEndpoint('https://cpq.com/rs/8/cpq'); 
        req.setHeader('Content-Type', 'application/json');
        req.setBody(jsonOrders1);                     
        req.setCompressed(true);
        res = http.send(req);
        
        Status code = 400;
        Message: content was not a valid JSON object
        
        I tested the same Post request using the Postman plugin in Chrome and it successfully updated the Account on the external system.
        Below is the JSON Body which I used in the Postman; when I print my json request in Salesforce, I am getting the similar JSON response.
        Can anyone help in trouble shooting this issue. What am I doing wrong in creating the JSON request.
        
        JSON used in Postman:
        [
             {"Id":"0ca0000f8pxds72r","Name":"TEST123"}
        ]  
        
        JSON when I print in debug logs in Salesforce:
         [ { "Id" : "0ca0000f8pxds72r",  "Name" : "TEST12345"} ]
        
        I was referring their api documentation. Look under the CPQ OnDemand Web Services API Calls (RESTful) header (Create/Update object)
        http://docs.fpx.com/docs/api/restful/14/
Omveer kundu 8Omveer kundu 8
Hi hemanth,
you cannot pass string value to httprequestbody
you should pass blob to httprequestbody.
 change 
req.setBody(jsonOrders1);

to
req.setBody(blob.valueof(jsonOrders1));

the final code will be:
HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        
        JSONGenerator gen = JSON.createGenerator(true);
        gen.writeStartArray();
        gen.writeStartObject();
        gen.writeStringField('Id', '0ca0000f8pxds72r');
        gen.writeStringField('Name', 'TEST12345');
        gen.writeEndObject();
        gen.writeEndArray();
        String jsonOrders1 = gen.getAsString();
        System.debug('jsonOrders: ' + jsonOrders1 );

        req.setMethod('POST');        
        req.setEndpoint('https://cpq.com/rs/8/cpq'); 
        req.setHeader('Content-Type', 'application/json');
        req.setBody(blob.valueof(jsonOrders1));                     
        req.setCompressed(true);
        res = http.send(req);

 
Hemanth BakthavatsaluHemanth Bakthavatsalu
Thanks for your reply, but i am getting compiler error in Salesforce:

Error: Compile Error: Method does not exist or incorrect signature: [System.HttpRequest].setBody(Blob) at line 110 column 9

 req.setBody(blob.valueof(jsonOrders1));  

I was referring the below example for my code:
https://developer.salesforce.com/docs/atlas.en-us.integration_workbook.meta/integration_workbook/integration2a_3.htm
 
Omveer kundu 8Omveer kundu 8
Hi,
then remove the code from the original code
gen.writeStartArray();
gen.writeEndArray();

final code will be:
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
        Http http = new Http();
        
        JSONGenerator gen = JSON.createGenerator(true);

        gen.writeStartObject();
        gen.writeStringField('Id', '0ca0000f8pxds72r');
        gen.writeStringField('Name', 'TEST12345');
        gen.writeEndObject();

        String jsonOrders1 = gen.getAsString();
        System.debug('jsonOrders: ' + jsonOrders1 );

        req.setMethod('POST');        
        req.setEndpoint('https://cpq.com/rs/8/cpq'); 
        req.setHeader('Content-Type', 'application/json');
        req.setBody(jsonOrders1);                     
        req.setCompressed(true);
        res = http.send(req);

or you can use this:
HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        
        
        String jsonOrders1 = '{ "Id" : "0ca0000f8pxds72r",  "Name" : "TEST12345"}';
        

        req.setMethod('POST');        
        req.setEndpoint('https://cpq.com/rs/8/cpq'); 
        req.setHeader('Content-Type', 'application/json');
        req.setBody(jsonOrders1);                     
        req.setCompressed(true);
        res = http.send(req);
Deepak GulianDeepak Gulian
Error is that content is not valid
"0ca0000f8pxds72r" You are using 16 digit id which is not valid ID, ID should be 18 digit or 15 digit.
Omveer kundu 8Omveer kundu 8
yup Right Deepak Id Should be 15 or 18 Digits .Hope Its working
Hemanth BakthavatsaluHemanth Bakthavatsalu
Thanks all for your inputs...

The account id is not  the Salesforce account  id; I am trying to update the Account in the external system (CPQ); The id the account id in the external system.

I tried all the suggestions mentioned above, but still getting the same error; also I tried the below code snippet as well, but still no luck:

JSONGenerator gen = JSON.createGenerator(true);

gen.writeStartArray();
for(Integer i =0; i< 1; i++)
{
gen.writeStartObject();
gen.writeStringField('Id', '0ca0000f8pxds72r');
gen.writeStringField('Name', 'TEST12345');
gen.writeEndObject();
}
gen.writeEndArray();

String jsonOrders1 = gen.getAsString();

Following the exact steps as mentioned in the example below:
https://developer.salesforce.com/docs/atlas.en-us.integration_workbook.meta/integration_workbook/integration2a_3.htm