In this blog we will learn about Composite Resources In Salesforce. By using composite resources we can make multiple request in single REST API call. Let’s take an example: suppose we have to create a new product and pricebookentry . If we go step by step, we have to make 3 REST API call as follows:
1). First REST API call to create new product.
2). Second REST API call to get Id of created product.
3). Third, REST API call to make an PricebookEntry of this product.
By using composite resources, we can complete this simple task in single REST API call with a request body that contains all the logic.
Types of composite resources:
Salesforce provide three types of composite resources:
1). composite/batch(Batch):
–> Upto 25 subrequest in a single request can be executed.
–> Subrequests are executed independently, means information cannot be passed between subrequest call.
–> Each subrequest count as single request towards API limits.
2). composite/tree(sObject Tree):
–> An sObject tree is a collection of nested, parent-child records with a single root record.
–> All tree root record must be same type.
3). composite/(Composite):
–> Series of REST API call are executed subsequently in single REST API call.
–> Output of one subrequest can be used as input of subsequent subrequests.
–> Entire request count as single request towards API limits.
Note: The composite resources shouldn’t be used in place of existing APIs that are designed to handle asynchronous uploads or modifications of large batches of records, like the Bulk API.
Example :
Let’s do some example in workbench.
1) Click on the link to open workbench: Workbench
2) Log in to your Salesforce Org, and allow access.
3) Go to Utilities || REST Explorer
Example : #Batch:
Request Body: First we use PATCH method to update the existing Account Name, then we use GET method to get the updated Account detail.
{ "batchRequests" : [ { "method" : "PATCH", "url" : "v34.0/sobjects/account/0017F000007zCvC", "richInput" : {"Name" : "Webkul"} },{ "method" : "GET", "url" : "v34.0/sobjects/account/0017F000007zCvC?fields=Name,BillingPostalCode" }] }
Response Body :
“hasErrors”:false,
“results”:[
{
“statusCode”:204,
“result”:null
},
{
“statusCode”:200,
“result”:{
“attributes”:{
“type”:“Account”,
“url”:“/services/data/v34.0/sobjects/Account/0017F000007zCvCQAU”
},
“Name”:“Webkul”,
“BillingPostalCode”:“66045”,
“Id”:“0017F000007zCvCQAU”
}
}
]
}
Support
That’s all for Composite Resources In Salesforce , still if you have any further query feel free to contact us, we will be happy to help you https://wedgecommerce.com/contact-us/.