December 15, 2020

Composite Request in Salesforce

Composite API improves the REST API call performance. It avoids multiple round trips to a server. Instead, it makes a single call with multiple subrequests.

allOrNone 
The request body uses this allOrNone flag. It specifies how to roll back errors. If this flag is set to true, entire request is rolled back. If it is set to false, the remaining subrequests that don’t depend on the failed subrequests are executed. In both the cases, the top-level request returns HTTP response as 200 and contains responses for all the subrequests.

collateSubrequests   
Use this to bulkify the subrequests. When this is set to true, the processing will be faster. But, the order of execution is not guaranteed. If this is set to false, the subrequests are processed in the order in which they are received. Use this option if there is no dependencies in the requests.
 
Advantages:
1. Executes a series of REST API requests in a single API call.
2. The response bodies and HTTP statuses of the requests are returned in a single response body.
3. The entire request counts as a single call towards the Salesforce API limits.

For more information, check https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_composite_composite.htm
 
Workbench tool can be used to test the Composite Request API.
1. Path URL - /services/data/vxx.0/composite
2. HTTP Method - POST
 
Check the below Sample Request for example.
 
Sample Request:
{
    "compositeRequest" : [
        {
            "method" : "POST",
            "url" : "/services/data/v50.0/sobjects/Account",
            "referenceId" : "refAccount",
            "body" : { "Name" : "Sample Account" }
        },{
            "method" : "POST",
            "url" : "/services/data/v50.0/sobjects/Contact",
            "referenceId" : "refContact",
            "body" : {
                "LastName" : "Sample Contact",
                "AccountId" : "@{refAccount.id}"
            }
        },{
            "method" : "POST",
            "url" : "/services/data/v50.0/sobjects/Opportunity",
            "referenceId" : "refOppty",
            "body" : {
                "Name" : "Sample Oppty",
                "AccountId" : "@{refAccount.id}",
                "CloseDate" : "2020-12-15",
                "StageName" : "Prospecting"
            }
        }
    ]
}

 
Output:

No comments:

Post a Comment