Monday, June 26, 2023

Composite Batch- Way to call multiple REST API in single request (sending multiple request to Salesforce)

Composite batch allows to send up to 25 separate API request in a single call to salesforce. Best thing about this is that all sub-request are considered as separate call. Suppose you want to move multiple records to next salesforce org or want to create multiple records in salesforce from external system then this will help you.


By using composite batch API, I am going to create a new Account record, Update existing Account record (by using external Id field -Account_Unique_Id__c) and query account record in same API call.

Below are details you need:

REST API service URI-   /services/data/v56.0/composite/batch/

Request Body Sample-

{

"batchRequests": [{

"method": "POST",

"url": "v56.0/sobjects/Account",

"richInput": {

"Name": "New Account using Composite Batch"

}

},

{

"method": "PATCH",

"url": "v56.0/sobjects/account/Account_Unique_Id__c/ACC-000033",

"richInput": {

"NumberOfEmployees": "2000",

"Type": "Customer"

}

}, {

"method": "GET",

"url": "v56.0/query/?q=SELECT+name+from+Account+where+Account_Unique_Id__c='ACC-000033'"

}

]

}


Response Body:

{

"hasErrors": false,

"results": [{

"statusCode": 201,

"result": {

"id": "0010K00002mziKJQAY",

"success": true,

"errors": []

}

}, {

"statusCode": 200,

"result": {

"id": "0010K00002PBQTyQAP",

"success": true,

"errors": [],

"created": false

}

}, {

"statusCode": 200,

"result": {

"totalSize": 1,

"done": true,

"records": [{

"attributes": {

"type": "Account",

"url": "/services/data/v56.0/sobjects/Account/0010K00002PBQTyQAP"

},

"Name": "Sunil Test Account"

}]

}

}]

}


Important points to consider while using this:

  • The response bodies and HTTP statuses of the subrequests in the batch are returned in a single response body.
  • Each sub request counts against rate limits.
  • Each API request are considered as separate and you can not pass information between them.
  • If one API request get successfully completed from batch request, then it gets committed. If any subsequent request fails then previous request is not rollbacked automatically.
  • Batch request should complete in 10 minutes. If batch times out then the remaining sub requests aren’t executed

Hope this will help!!



No comments:

Post a Comment