0

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)

1
  • 3
    Your JSON is a valid array, but not a valid object. It seems it is really picky about the whole body being an object (which maybe contains an array?) Feb 26 '16 at 0:01
0

As is outlined in the comments (please vote their answer as the answer if they post), you need to wrap your JSON as an object, not an array. This should be pretty simple to do:

JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStartArray();
gen.writeStartObject();
gen.writeStringField('Id', '0ca0000f8pxds72r');
gen.writeStringField('Name', 'TEST12345');
gen.writeEndObject();
gen.writeEndArray();
gen.writeEndObject();
String jsonOrders1 = gen.getAsString();
1
  • Thanks for your reply, but still getting the same error. I tried the way that you suggested above. Also tried below code snippet as well: 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();
    – user5352
    Feb 26 '16 at 4:03

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.