Some notes about using the /composite API to do up to 25 statements in a single API call.
I am using this to do an integration between a Website and Salesforce. The client is an NFP and doesn't have budget to get a dev to to Custom Apex REST.
So, I read the docco and did a nice piece of pseudo code referencing all fields in Salesforce and matching it up with the fields in the Website, I thought it was going to work like a charm.
My pseudo code (yes, with all the errors in it)
path = "/services/data/42.0" // On Update of an existing Contact's details or creation of a new Contact //If SFContactID is blank { "method" : "PATCH", "url" : path + "/sobjects/Account/UniqueID__c/ABCEnterprisesNSW", //Company Name+(Billing Address State or Other State) with spaces stripped out then trimmed to 255 chars "referenceID" : "refNewAccount", "body" : { "Name" : "ABC Enterprises", //Company "BillingStreet" : "15 Smith Street", //Billing Address - Street "BillingCity" : "Smithville", //Billing Address - Suburb "BillingState" : "NSW", //Billing Address - State or Other State "BillingPostalCode" : "2656", //Billing Address - Postcode "BillingCountry" : "Australia", //Billing Address Country "Phone" : "02 9898 9898" //Business Phone } }, //Now get that Account ID just created or updated { "method" : "GET", "url" : path + "/sobjects/Account/@{refNewAccount.id}", "referenceId" : "refNewAccountInfo" }, // Now Create or Update the Contact { "method" : "PATCH", "url" : path + "/sobjects/Contact/UniqueID__c/JaneDoejdoe@gmail.com", //First Name + Surname + Email Address with spaces striped out then trimmed to 255 chars "referenceId" : "refNewContact", "body" : { "Saluation" : "Ms", //Title "FirstName" : "Jane", //First Name "LastName" : "Doe", //Surname "Title" : "Director", //Position "Email" : "jdoe@gmail.com", //Email Address "AccountId" : "@{NewAccountInfo.Id}", "MailingStreet" : "@{NewAccountInfo.BillingStreet}", "MailingCity" : "@{NewAccountInfo.BillingCity}", "MailingState" : "@{NewAccountInfo.BillingState}", "MailingPostalCode" : "@{NewAccountInfo.BillingPostalCode}", "MailingCountry" : "@{NewAccountInfo.BillingCountry}", "Phone" : "@{NewAccountInfo.Phone}", "MobilePhone" : "9908960940" //Mobile } }, //Now get that Contact ID just created or updated { "method" : "GET", "url" : path + "/sobjects/Contact/@{refNewContact.id}", "referenceId" : "refContactInfo" }, // Update that into your SFContactID field on the website |
Project go ahead happens and the first thing I do is fix up my pseudo code to test it in Workbench. Oops, I got so much wrong - apart from the spelling mistakes like 'Saluation' rather than 'Salutation'.
Gotchas
"method" : "PATCH", "url" : "/services/data/v41.0/sobjects/Account/UniqueID__c/ABCEnterprisesVIC", "referenceId" : "refNewAccount", "body" : { "Name" : "ABC Enterprises", "BillingStreet" : "15 Smith Street", "BillingCity" : "Smithville", "BillingState" : "VIC", "BillingPostalCode" : "2656", "BillingCountry" : "Australia", "Phone" : "02 9898 9898", } }, { "method" : "PATCH", "url" : "/services/data/v41.0/sobjects/Contact/UniqueID__c/JaneDoejdoe@gmail.com", "referenceId" : "refNewContact", "body" : { "Salutation" : "Ms", "FirstName" : "Jane", "LastName" : "Doe", "Title" : "Director", "Email" : "jdoe@gmail.com", "AccountId" : "@{refNewAccount.id}" } |
Solution
{ "compositeRequest" : [ { "method" : "PATCH", "url" : "/services/data/v41.0/sobjects/Account/UniqueID__c/ABCEnterprisesNSW", "referenceId" : "refNewAccount", "body" : { "Name" : "ABC Enterprises", "BillingStreet" : "15 Smith Street", "BillingCity" : "Smithville", "BillingState" : "NSW", "BillingPostalCode" : "2656", "BillingCountry" : "Australia", "Phone" : "02 9898 9898" } }, { "method" : "GET", "url" : "/services/data/v41.0/query/?q=select+id,name,Industry,BillingStreet,BillingCity,BillingState,BillingPostalCode,BillingCountry,Phone+from+Account+where+UniqueID__c='ABCEnterprisesNSW'", "referenceId" : "refNewAccountInfo" }, { "method" : "PATCH", "url" : "/services/data/v41.0/sobjects/Contact/UniqueID__c/JaneDoejdoe@gmail.com", "referenceId" : "refNewContact", "body" : { "Salutation" : "Ms", "FirstName" : "Jane", "LastName" : "Doe", "Title" : "Director", "Email" : "jdoe@gmail.com", "AccountId" : "@{refNewAccountInfo.records[0].Id}", "MailingStreet" : "@{refNewAccountInfo.records[0].BillingStreet}", "MailingCity" : "@{refNewAccountInfo.records[0].BillingCity}", "MailingState" : "@{refNewAccountInfo.records[0].BillingState}", "MailingPostalCode" : "@{refNewAccountInfo.records[0].BillingPostalCode}", "MailingCountry" : "@{refNewAccountInfo.records[0].BillingCountry}", "Phone" : "@{refNewAccountInfo.records[0].Phone}", "MobilePhone" : "9908960940" } }, { "method" : "GET", "url" : "/services/data/v41.0/query/?q=select+id+from+Contact+where+UniqueID__c='JaneDoejdoe@gmail.com'", "referenceId" : "refNewContactId" }] } |