1

I am trying to insert 2 records at a time using canvas client in a connected app, using REST API, code and response below:

Sfdc.canvas.client.ajax('/services/data/v47.0/composite/sobjects',{
   "allOrNone" : false,
   client: Sfdc.canvas.oauth.client(), method: 'POST',
       "records" : [{
          "attributes" : {"type" : "Account"},
          "Name" : "example.com",
          "BillingCity" : "San Francisco"
       }, {
          "attributes" : {"type" : "Contact"},  
          "LastName" : "Johnson",
          "FirstName" : "Erica"
       }],
   success: function(data){console.log(data.payload)}
})

Getting an error - "The HTTP entity body is required, but this request has no entity body."

But I am able to insert 1 record at a time with the code below.

Sfdc.canvas.client.ajax('/services/data/v47.0/sobjects/account/',{
       client: Sfdc.canvas.oauth.client(),
    method: 'POST', data: '{"Name" : "California"}',
    success: function(data){console.log(data.payload)}
})

Any recommendation? Tried other implementations as described in

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_composite_sobjects_collections_create.htm

and

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_composite_sobject_tree_flat.htm

1 Answer 1

2

It seems like you are missing the data attribute in your JS Code. Also you should be using the /composite/tree/{sobject} resource as well based on the JSON you were trying to send. Here is an example

var postData = {
"records" :[
  {
    "attributes" : {"type" : "Account", "referenceId" : "ref1"},
    "name" : "SampleAccount1",
    "phone" : "1111111111",
    "website" : "www.salesforce1.com",
    "numberOfEmployees" : "100",
    "industry" : "Banking"
  },{
    "attributes" : {"type" : "Account", "referenceId" : "ref2"},
    "name" : "SampleAccount2",
    "phone" : "2222222222",
    "website" : "www.salesforce2.com",
    "numberOfEmployees" : "250",
    "industry" : "Banking"
  }]
};

Sfdc.canvas.client.ajax('/services/data/v47.0/composite/tree/Account',{
   "allOrNone" : false,
    client: Sfdc.canvas.oauth.client(),
    method: 'POST',
    data: JSON.stringify(postData),
   success: function(data){console.log(data.payload)}
})
2
  • 1
    Thanks much Bryan for your quick response. Did a trick with json stringify on postData and the above code to solve it.
    – Akshay
    Nov 22, 2019 at 18:12
  • Ah yeah sorry forgot to add that in Nov 22, 2019 at 18:23

You must log in to answer this question.

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