I am trying to update and insert (upsert) a bunch of records into Salesforce. The records are in a json file on my hard-drive. The records are inserted but if they already exist, they need me to specify Salesforce "Id" as a column. How do I retreive Id from Salesforce and then add to my json file so that it updates the records.

Context: REST API is being used for this operation. I've configured a .NET client application to do this.


JObject job = JObject.Parse(File.ReadAllText(@"C:\Users\vinaytemb\accounts1rec.json"));

String x = job.ToString();

String restURL = serviceUrl + "/services/data/v45.0/composite/sObjects/";

HttpRequestMessage rmess = new HttpRequestMessage(new HttpMethod("PATCH"), restURL);
  • You'll need to correlate two lists of records and doing so in JSON is painful. Translate JSON to CSV, there are a million tools for this, then use Excel or whatever to correlate, and finally use Data Loader to upsert. Much simpler. – identigral Sep 11 '19 at 17:49
  • Thanks for the tip! Can I correlate in code by using a simple matching if function? – Vinae Temburnikar Sep 11 '19 at 17:53
  • Sure, the correlation rules will be the same regardless of the tool. – identigral Sep 11 '19 at 18:03
  • Thank you for the answer – Vinae Temburnikar Sep 11 '19 at 18:05

I am trying to update and insert (upsert) a bunch of records into Salesforce

Salesforce already has a native concept of upserting. This includes using an external ID reference field on the sObject to identify if it should be and update or insert operation.

Presumably you already have the data for the external ID field in your JSON file. E.g. you would have needed this to find the existing records in Salesforce by a SOQL query to populate the ID's anyway.

There are more details on using the composite resource to upsert multiple records at a time in Upsert Multiple Records with external ID with REST API

| improve this answer | |
  • Hi Daniel. I know how to query the existing records, but I am having a hard time linking them to the json file to populate Ids. Is there a way I can substitute this since I already have an unique id (external id) in my json? – Vinae Temburnikar Sep 12 '19 at 14:34
  • @VinaeTemburnikar Upserting is specifically designed to handle your scenario where you already have the unique external ID in the JSON file. Have a look at the documentation I linked to. You include the name of the field in the path that contains the external ID. After that you include the external ID at the end of the path. Salesforce will use that to determine if the operation should be an insert or update. – Daniel Ballinger Sep 16 '19 at 20:42

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.