Salesforce REST API: How to Execute Batch of REST API Requests using Composite Batch Resource


Looking for Salesforce Training & HandsOn Projects?

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


Salesforce REST API Framework provides many powerful implementations that can help to boost an application’s performance significantly. 

 

“Composite” resources are one such implementation offering that helps improving Salesforce Application’s performance by minimizing the round-trips between the Client and Server.

 

In this article, we will discuss the “composite/batch” resource, which can execute up to 25 subrequests in a single request. It will return the HTTP response body and Status Code for each of the subrequests in the batch within the single response body. 

 

It is important to note that each subrequest count against the API Limits. So be proactive to plan the solution approach accordingly.

Lets’ hit the road to see some action.

 

Prepare Salesforce Metadata for Demo

 

To proceed with the demo, I have added some of the SObjects and fields to them respectively. 

 

Step-1: Added “Invoice__c” custom SObject that will store the Invoices for the Customers as shown below



Step-2: Added “Amount, Customer (Lookup of Account Object), Description, Invoice Date, Invoice Number” fields to “Invoice__c” object as shown below



Step-3: Added “Payment__c” custom SObject that will store the transactions related to the payments made by the Customers as shown below



Step-4: Added “Amount, Customer (Lookup of Account Object)” fields to “Payment__c” object as shown below



Add Test Data to Objects

 

Step-5: Add test records to “Account” Object


Step-6: Add test records to “Invoice” Object according to Account Lookup relationship with Invoices



Step-7: Add test records to “Payment” Object according to the Account Lookup relationship with Payment Transactions



Execute “composite/batch” request using Postman


Launch Postman or any similar tool of your choice. Postman is my favorite tool when it comes to API Testing.

Step-8: This is going to be a post request so select “POST” as the type of HTTP request

 

Step-9: Here we can take note of the format convention used to prepare the requested URL

 

<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-10: Select “Headers” to add the HTTP Headers to the request

 

Step-11: 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-12: Select “Body” to add request body in Json format, then select “raw” since this a raw input (Plain text Json)

 

Step-13: Add a list of “GET” Requests as part of the request body in JSON format. This list is quite flexible for example “method” could be “GET” or “POST”, “URL” could be of any SObject you like to act upon. Refer to the screenshot below for better understanding

 

Step-14: Click on the “Send” button to execute 



Analyze Response

 

Assuming that we got the request executed successfully, we can see the response coming in. So lets’ roll over and dig deeper to know better

 

Step-15: “hasErrors” property would tell us if the request execution at Server was a success or not. If it has a value “false” then Server execution was a success else there was an error occurred during the execution

 

Step-16: “results” object is an array of responses for each Sub Request from the batch. The first block holds the response for “Account” Object. 

 

In this block of data, we can see the information that we queried or created during the batch request. “Name, Id” Fields are returned as part

 

Step-17: This block of data for “Invoice__c” which is a custom SObject is another part of the response body that is returned since we have a request for Invoice Object inside of the request batch. 

 

In this response block we can see “Name, Invoice_Date__c, Description__c, Customer__r (based on lookup relationship between Accounts & Invoice__c Object), Customer__r.Name” fields returned



Step-18: This block of data for “Payment__c” which is another custom SObject and is part of the response body which is returned since we have a request for Payment Object inside of the request batch. 

 

In this response block we can see “Name, Customer__r (based on lookup relationship between Accounts & Payment__c Object), Customer__r.Name” fields returned



A Bit of Perspective

 

“Composite” resources is one of the most powerful implementations done around Salesforce REST API Framework that can be used to boost the applications’ performance significantly since they support the mechanism of executing a batch of requests on the Server and it helps improving Salesforce Application’s performance by minimizing the round-trips between the Client and Server.

Hope you enjoyed this article. Please leave your comments to let me know how you do like the content and how you do find it helpful to learn the topic.

Comments