Salesforce REST API: How to Execute Bulk API Operations Using Composite Batch Resource






Looking for Salesforce Training & HandsOn Projects?



Composite resource offered by Salesforce allows used to execute a series of REST API Requests in single call. Each of the child request is called as Subrequest. After the execution we will get Response Bodies & corresponding HTTP status for each subrequest with in the same call.

Using this design pattern has a huge advantage of reducing number of API Calls to Salesforce. Since the whole series of requests counts as single call towards the API Limits.

Possibilities are limitless so plan your solutions using the best design pattern at hand for the given piece of problem.

In this blog we will discuss the implementation of “Composite/Batch” design pattern to solve a business problem.

To start with this demo I am having a scenario where I need to Post some “Invoices” and “Payments” for a Customer, also need to retrieve them and that’s all in the single API call.

So using “Composite/Batch” design pattern would be advisable since it perform Series of REST API Requests in one single call.

Step-1 & 2: We have a custom object “Invoice” that will hold invoice information about the Customer


Step-3 & 4 & 5: We can see the list of custom field available with in “Invoice” Object

Step-6 & 7: Similarly we have another Custom Object “Payment” that will hold the Payment details corresponding to the particular Customer

Step-8 & 9 & 10: We can see the list of custom field available with in “Payment” Object

Now since we have reviewed the metadata available for this scenario, we can move on to build “Composite/Batch” request. Tools may change but steps remains the same. Here I am making use of Postman to build and execute the API call

Step-11: This request will always going to be a “POST” request

Step-12: Specify the resource Endpoint as follows-

<Salesforce Instance Address>/services/data/v<Apex API Version>/composite/batch>

That will translate to the following URL that I am using for this demo 

https://bansalp-dev-ed.my.salesforce.com/services/data/v47.0/composite/batch

 

Step-13: Select “Headers” to add the HTTP Headers to the request 

Step-14: Add the following headers 

“Content-Type”: “application/json”

“Authorization”: “<Enter authorization Token Here” 

You can retrieve the authorization token that we retrieved from The Login Request



Step-15: Select “Body” to add request body in Json format and select “raw” since this a raw input (Plain text Json) 


Step-16: In the body, we need to add the request body which should be a valid JSON as shown below


Step-17: We can see how we are adding a “POST” Subrequest that is going to add new record for the Invoice Object. Just below that we can also see how we are adding a “GET” Subrequest to read data from Invoice Object

Step-18: Similarly we can see how we are adding another “POST” Subrequest to add new record for Payment Object. Just below that we can also see how we are adding a “GET” Subrequest to read data from Payment Object

With this we have completed building above request and now we can execute.

Step-18.1: To execute the request, click the “Send” button


Step-19: Here we can review the status of execution that is returned as part of the Response Object. We can see “hasErrors: false” showing the execution was a success.

Step-20: We can see the response of one of the “POST” Subrequest we execute earlier. In this vital piece of information we can see “statusCode”, “id” of the new record created for the object, “success” flag, “errors” array of all errors occurred during the executions, helpful to resolve the request later

Now following steps explains the Response Object for GET Subrequests-

Step-21: “statusCode” shows us if the request executed successfully or not. Status Code = “200” shows a success

Step-22 & 23: Shows details of the Object corresponding to the “GET” Subrequest. Here in this example this is the “GET” request for Invoice Object


Step-24 & 25: Here we can see the list of “Recent Items” added or modified for the object

Step-26 & 27 & 28: Similarly we will get details for any other “GET” Subrequest mentioned in the batch call. Here in this example another “GET” Subrequest was for Payment Object and we can see the response object for the same

Step-29 & 30: Here we can see the list of “Recent Items” added or modified for the object


Step-31 & 32:
 We can see the results of the Batch Call execution by getting to the Salesforce UI. Here we can see the “Payment” Object and records added to it by the Batch Call



Step-33 & 34 & 35 & 36 & 37 & 38 & 39: Here we can see the “Invoice” Object and records added to it by the Batch Call




 Conclusion

As we can how powerful is “Composite/Batch” design pattern when we have the requirement to execute multiple REST Requests as part of the Single call to Salesforce.

It is also important to notice that we can have only up to 25 Subrequests included in a single call. And out of these 25 Subrequests we can have 5 GET requests that can query collection of records from SObjects with the support of Query and QueryAll options.

This design pattern also supports dependent sObject Calls by making use of “referenceId” parameter and that help you to reuse the reference of a parent to query its child records.

For example, we can find all the Accounts and their respective Contacts using the same bulk call.

Trailblazer Profile | LinkedIn | Salesforce Blog | Facebook | Youtube Channel | WhatsApp Community


Comments