You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Use Composite API Instead of Apex Rest Class


By: Radha

Introduction:   

Salesforce introduced a new feature in Spring ’17 called Composite AP. This API provides an option to make multiple combined Callouts to the Salesforce, and we can refer the values between each node in the body request. For Example, if an account was created by using the REST API call and If you want to create 10 Contacts upon the creation of the Account, you must write a trigger in the Account but by using the composite API, you can create the Contacts along with the Account in a single API call.
 
Sample Payload for Composite API: 

{ 

  "allOrNone": true, 

  "compositeRequest": [ 

    { 

      "method": "POST", 

      "url": "/services/data/v38.0/sobjects/Account", 

      "referenceId": "NewAccount", 

      "body": { 

        "Name": "SF-composite Account", 

        "Phone": "987654321" 

      } 

    }, 

    { 

      "method": "GET", 

      "referenceId": "NewAccountInfo", 

      "url": "/services/data/v38.0/sobjects/Account/@{NewAccount.id}" 

    }, 

    { 

      "method": "POST", 

      "url": "/services/data/v38.0/sobjects/Contact", 

      "referenceId": "newAccountv", 

      "body": { 

        "lastname": "@{NewAccountInfo.Name} - @{NewAccountInfo.Phone}", 

        "AccountId": "@{NewAccountInfo.Id}", 

        "Phone": "@{NewAccountInfo.Phone}" 

      } 

    }, 

    { 

      "method": "POST", 

      "url": "/services/data/v38.0/sobjects/Contact", 

      "referenceId": "newAccountv1", 

      "body": { 

        "lastname": "@{NewAccountInfo.Name} - @{NewAccountInfo.Phone}", 

        "AccountId": "@{NewAccountInfo.Id}", 

        "Phone": "@{NewAccountInfo.Phone}" 

      } 

    } 

  ] 

} 

The above payload is used to Create an Account and then two Contacts under that account.
In the first node, Post method is used to create an account with the details in the body and in the second node GET method would pick the Account information (which is created in the above node) and then using the “referenceId”, we can refer that value in the below nodes to perform any operation.
You can have up to 25 sub requests in a single call. Up to 10 of these sub requests can be query operations, including Query, QueryAll, and “Query More” requests to obtain the next batch of query results.

Snippet to create more than 2 contact records under a single account in a Single API call

HttpRequest req = new HttpRequest(); 
String bodyval = '{"allOrNone":true,"compositeRequest":[{"method":"POST","url":"/services/data/v38.0/sobjects/Account","referenceId":"NewAccount","body":{"Name":"New-composite Account","Phone":"987654321"}},{"method":"GET","referenceId":"NewAccountInfo","url":"/services/data/v38.0/sobjects/Account/@{NewAccount.id}"},{"method":"POST","url":"/services/data/v38.0/sobjects/Contact","referenceId":"newAccountv","body":{"lastname":"@{NewAccountInfo.Name} - @{NewAccountInfo.Phone}","AccountId":"@{NewAccountInfo.Id}","Phone":"@{NewAccountInfo.Phone}"}},{"method":"POST","url":"/services/data/v38.0/sobjects/Contact","referenceId":"newAccountv1","body":{"lastname":"@{NewAccountInfo.Name} - @{NewAccountInfo.Phone}","AccountId":"@{NewAccountInfo.Id}","Phone":"@{NewAccountInfo.Phone}"}}]}'; req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID()); req.setHeader('Content-Type', 'application/json'); req.setHeader('Content-Length', '0'); String domainUrl=URL.getSalesforceBaseUrl().toExternalForm(); system.debug('********domainUrl:'+domainUrl); req.setEndpoint(domainUrl+'/services/data/v38.0/composite/'); req.setMethod('POST'); req.setBody(bodyval); Http h = new Http(); HttpResponse res = h.send(req); system.debug(res.getBody());

 

Apex Rest class Vs Composite API 

  1. It is common that we use the Apex Rest classes to perform complex DML operations, but we can do the same thing using the composite API by creating the sub requests appropriately (we can do POST, PUT, DELETE, PATCH, GET in Single API Call).
  2. We normally manage Apex Rest classes and migrate them to production instance to process the messages from the other System but using the Composite API, we don’t need to manage any classes and we can directly call the Salesforce Standard API. That’s all! The sub requests will take care the operations in the System.

Reference Link: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_composite.htm

Conclusion: 

Salesforce introduced the Composite API to make the callouts parallelly. This would reduce time because we need to only construct the payload. Non-developers can also easily do the complex DML operations using the Composite API.