+ Start a Discussion
Fenil Suthar 007Fenil Suthar 007 

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()))
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 :

Error in .net Code : 400 bad request
Error in Workbench : errorCode: METHOD_NOT_ALLOWED
message: HTTP Method 'POST' not allowed. Allowed are HEAD,GET

ShashankShashank (Salesforce Developers) 
You can't create/update multiple records in a single call via the REST API. You can find more info here: http://salesforce.stackexchange.com/questions/21478/is-it-possible-to-insert-multiple-records-in-salesforce-using-curl-at-the-same-t
Hi Fenil Suthar 007,
The REST API you are using does not support bulk insert. To create bulk records using REST API you need to use Bulk API. Here I would explain ho to do it:

Authorize with Salesforce: Use your authentication method to authorize yourself with Salesforce.

Create bulk API job in Salesforce: 
URL - /services/data/v41.0/jobs/ingest/
HTTP Method - POST
Content-Type - application/json

Request Body - 
{ "operation" : "insert", "object" : "Opportunity", "contentType" : "CSV", "lineEnding" : "CRLF" }

You will receive a response like this:
    "id": "7503M000000****QAA",
    "operation": "insert",
    "object": "SHT_Dave_Master__c",
    "createdById": "0052X00000####LQAQ",
    "createdDate": "2020-05-08T07:52:35.000+0000",
    "systemModstamp": "2020-05-08T07:52:35.000+0000",
    "state": "Open",
    "concurrencyMode": "Parallel",
    "contentType": "CSV",
    "apiVersion": 41.0,
    "contentUrl": "services/data/v41.0/jobs/ingest/7503M000000****QAA /batches",

Set data into CSV format for the job
Once you are done with creating the job, the next step would be setting up data that you want to insert into the created bulk job. To do that prepare data in CSV or use https://www.convertcsv.com/json-to-csv.htm (https://www.convertcsv.com/json-to-csv.htm" style="color:blue; text-decoration:underline) to convert your existing data into CSV
URL - /services/data/v41.0/jobs/ingest/7503M000000****QAA/batches      
(Here 7503M000000****QAA is you job iid return from last response)
HTTP Method - PUT
Content-Type - text/csv

Request Body –  Your CSV data

Start the bulk job
Once you are done with setting up the job. The last step would be notifying Salesforce to start the job.
URL - /services/data/v41.0/jobs/ingest/7503M000000****QAA/
Content-Type – application/json

Request Body – 
             "state" : "UploadComplete"

Your are done! To check your job status, use this method:
URL - /services/data/v41.0/jobs/ingest/7503M000000****QAA/
HTTP Method - GET
Content-Type – application/json

Go to https://trailhead.salesforce.com/en/content/learn/modules/api_basics/api_basics_bulk for the example.