Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Current »

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

  • referenceId must be spelt referenceId, not referenceID
  • Doing a PATCH on Account does not return the referenceId if it does not create a new Account. This seems weird to me, but I'm going to have to run with it for now. 
    • The refNewAccount.id will not work if the Account is updated, only if it is created. 
	"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}"
   }
  • Doing a GET on an id returns the whole object, not just the ID
    • "method" : "GET",
      "url" : "/services/data/v41.0/sobjects/Contact/@{refNewContact.id}",
      "referenceId" : "refContactInfo"


Solution

  • So you have to do a GET with a query in the middle of the two PATCH methods. 
  • AND you can't use {{@{refNewAccount.id}}} syntax, you have to use the extended query syntax. 
  • So this is what finally worked!
{
"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"                                
    }]
}


  • No labels