[SalesForce] How to create bulk record using rest API

Example for creating an Opportunity :

curl https://na1.salesforce.com/services/data/v20.0/sobjects/Opportunity/ -H "Authorization: Bearer token" -H "Content-Type: application/json" -d @newrecord.json -X PATCH

Example request body newrecord.json file :

{ "Name":"FFNEw","CloseDate":"2015-05-02","StageName":"Prospecting","Probability":10 }

My ASP.net code :

using (WebClient client = new WebClient())
{
client.Headers.Add("Authorization", "Bearer " + token.access_token); client.Headers.Add("Content-Type", "application/json");
var request = (HttpWebRequest)(HttpWebRequest.Create(token.instance_url + "/services/data/v20.0/sobjects/Opportunity/"));
request.Method = "POST";
using (var requestWriter = new StreamWriter(request.GetRequestStream()))
{
  requestWriter.Write(json);
  requestWriter.Flush();
  requestWriter.Close();
}
var response = request.GetResponse();
}

I am able to create a single opportunity using above code. But when i try to create multiple record using below "newrecord.json" file getting error.

Example request body for multiple record newrecord.json file :

[{"Name":"Opp1","CloseDate":"2015-05-02","StageName":"Stage1","Probability":"10","Amount":"1500"},{"Name":"Opp2","CloseDate":"2015-02-03","StageName":"Stage2","Probability":"5","Amount":"2000"}]

Error in .net Code : 400 bad request

Error in Workbench : errorCode: METHOD_NOT_ALLOWED
message: HTTP Method 'POST' not allowed. Allowed are HEAD,GET

Best Answer

The REST API doesn't provide a mechanism for updating multiple records simultaneously, like the Bulk or SOAP APIs do. You have two basic options: Custom REST Services, or Bulk/SOAP API.

If you use the Bulk API, you can post the data and query for the results later. If you use the SOAP API, you can post hundreds of records at once and get a real-time response. Using the REST API, you'll have to create records one at a time.

Related Topic