11

I am using cURL to insert data into salesforce. It works with single record but I need to insert multiple records at a time. Is it possible to do so and is it possible via a REST interface?

  • Hi, Could you please correct me with regard to the cURL command line that I'm using for inserting a record into Salesforce: curl -H "Authorization: Bearer sessionId" -H "Content-Type: application/json" -d @account.txt "instance.salesforce.com/services/apexrest/Account" I'm unable to do the insert as I'm unaware to use "account.txt" file, if I needed to give any path location to the above command line. By the way, I'm following the example provided in Salesforce salesforce.com/us/developer/docs/apexcode/Content/… – Sapthagiri Jan 4 '14 at 8:26
  • 1
    Hi Sapthagiri ,,Instead of account.txt use .json File Format ,because Here The Content Type you Mentioned is json Type . – Vineeth Jan 6 '14 at 5:50
  • When I'm sending the following cURL command, curl -H 'Authorization: Bearer access_token' -H 'Content-Type: application/json' -d @D:\notes.json 'ap1.salesforce.com/services/apexrest/Notes__c' client_id=client_id --form client_secret=secret I am getting the following error Warning: Couldn't read data from file "D:notes.json", this makes an empty Warning: POST. Warning: You can only select one HTTP request! – Sapthagiri Jan 6 '14 at 7:51
  • Here is a example curl -H "Authorization:OAuth 00D90000000kwMw!AQYAQE09Is.2ZGr7qvAqGPz8.0y1fnEfQJJluW3yAV9nPNtMw6M1OrnCKsMmGUPC495DU95Nq83sI3qja3m6NLo3TAdePlZ9" -H "Content-Type: application/json" -d @newaccount.json "ap1.salesforce.com/services/data/v20.0/sobjects/Account" – Vineeth Jan 6 '14 at 7:56
  • Hi Vineeth, do we need to have this "newaccount.json" at any particular location & how are we supposed to provide this path. Actually, I'm trying to execute these cURL commands in apigee.com/console & I'm struck with the following error message: { "error_description": "grant type not supported", "error": "unsupported_grant_type" } – Sapthagiri Jan 6 '14 at 8:27
9

You can't insert multiple rows in a single call in the rest api, you need to use the bulk api.

You can develop custom REST wrapper webservice through APEX language of sfdc to get this set up

@RestResource(urlMapping='/insertcontact/*')

global with sharing class Mycontacts{

 @HttpPost
  global static List<Contact> doPost(List<Contact> lstcnts){
      insert lstcnts;
      return lstcnts;
   }

}

This is pretty raw version.You can follow best practices and track successful Ids and failed records .

If there are two much of records i would suggest you to use BULK API of sfdc as the REST API has size limitations and all apex governor limits apply

Edit

As per latest update this is now possible with tree API provided

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

  • Hi mohith do you have any reference code for inserting multiple records using rest api ..it wl be helpfull if you do so ... – Vineeth Dec 1 '13 at 4:32
  • What do you mean by reference code here.The above code will work and you can test with curl.The url will be instanceurl/services/apexrest/insertcontact.Where instance url is your salesforce instance url – Mohith Shrivastava Dec 1 '13 at 6:13
  • Hi Mohith, I am inserting a single record with REST call.. How would be the json request if I want to insert the multiple records with REST .. – amidstCloud Nov 11 '14 at 6:35
  • Hi @MohithShrivastava Can you please update your answer with latest update s for creating multiple records using rest API . Please see this doc developer.salesforce.com/docs/atlas.en-us.api_rest.meta/… this will help other to understand this is possible now – Ratan Paul May 24 '16 at 6:49
3

As with my answer to How to Insert/Update multiple records in a single Force.com REST API call? there are some interesting details in Knowledge Article Number: 000214070 - Summer `15 - REST API Updates.

There is going to be a new Batch resource where you can send multiple sub-requests and get a separate sub-response back for each.

  • Thank you, Daniel. I almost started writing Apex REST API and came across this post. – Avinash Mar 28 '17 at 17:17
2

It's possible, please have a look at the official documentation with its example: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_composite_sobject_tree_flat.htm

0

As of API v42.2 you can now use the SObject Collections Composite endpoints. There are two major benefits over the existing composite tree & batch endpoints.

1: You can update up to 200 records at a time

2: The updates take place in a single execution context on salesforce. The other composite record create a context PER RECORD, which make it run incredibly slow for big updates.

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.