Introduction
In the world of Salesforce, APIs (Application Programming Interfaces) are the backbone of how different systems communicate with each other.
They allow you to connect Salesforce with other applications, automate processes, and build custom solutions. Among the many APIs Salesforce offers, the Composite API stands out as a powerful tool for developers.
But what exactly is it, and why should you care? Let’s break it down in simple terms
What is Salesforce Composite API ?
Imagine you’re building a house. Instead of bringing in one brick at a time, you’d want to bring in a whole stack of bricks to save time and effort.
The Salesforce Composite API works in a similar way.
It allows you to bundle multiple operations (like creating, updating, or deleting records) into a single API call.
This means you can perform several tasks in one go, rather than making multiple API calls.
In technical terms, the Composite API lets you combine multiple API requests into a single HTTP request. This reduces the number of API calls, improves performance, and ensures data consistency.
Why Use Composite API ?
Imagine you need to create an Account, a Contact under that Account, and an Opportunity linked to both.
Normally, you would make three separate API calls.
With Composite API, you can do all of this in just one request, saving time and API limits.
Key Features:
Multiple Operations in One Call
- Perform multiple CRUD (Create, Read, Update, Delete) operations in a single request.
Execution Order Control
- The requests are executed in sequence, so later operations can depend on earlier ones.
Reference IDs
- You can reference the response of one operation in a later operation within the same request.
Reduced API Calls
- Saves API limits by combining multiple operations.
Atomicity
- If one part of the request fails, the entire operation can be rolled back. This ensures data consistency.
Flexibility
- You can mix and match different types of operations (e.g., create a record, update another, and delete a third) in a single request.
How Does It Work?
Let’s say you’re building an app that needs to:
Create a new Contact.
Update an existing Account.
Delete an old Opportunity.
Without the Composite API, you’d need to make three separate API calls. With the Composite API, you can combine all these actions into one request.Â
Here’s a simplified example of how it might look:
{
"compositeRequest": [
{
"method": "POST",
"url": "/services/data/vXX.X/sobjects/Contact/",
"referenceId": "newContact",
"body": {
"LastName": "Doe",
"Email": "john.doe@example.com"
}
},
{
"method": "PATCH",
"url": "/services/data/vXX.X/sobjects/Account/001XXXXXXXXXXXXXXX",
"body": {
"Name": "Updated Account Name"
}
},
{
"method": "DELETE",
"url": "/services/data/vXX.X/sobjects/Opportunity/006XXXXXXXXXXXXXXX"
}
]
}
In this example:
-
-
TheÂ
POST
 method creates a new Contact. -
TheÂ
PATCH
 method updates an existing Account. -
TheÂ
DELETE
 method removes an old Opportunity.
-
All these actions are sent in one go, and Salesforce processes them in the order you specify.
When Should You Use Composite API?
The Composite API is ideal for scenarios where you need to:
Perform multiple related operations in a single transaction.
Reduce the number of API calls to improve performance.
Ensure data consistency by rolling back changes if something goes wrong.
However, it’s not always the best choice. For simple, one-off operations, using a standard REST API call might be more straightforward.
Tips for Using Composite API
Plan Your Requests: Since you’re bundling multiple operations, plan the order and dependencies carefully.
Handle Errors Gracefully: If one part of the request fails, the entire operation can fail. Make sure your app can handle this gracefully.
Stay Within Limits: Salesforce has API limits, so be mindful of how many operations you bundle in a single request.
Test Thoroughly: Composite API requests can be complex, so test them thoroughly to ensure they work as expected.
Conclusion
The Salesforce Composite API is like a Swiss Army knife for developers. It simplifies complex operations, reduces the number of API calls, and ensures data consistency.
Whether you’re building a new app or optimising an existing one, the Composite API can save you time and effort.
So, the next time you find yourself making multiple API calls for related tasks, consider bundling them together with the Composite API.
I hope this post helped you understand the Salesforce Composite API in simple terms.
If you have any questions or want to dive deeper, feel free to leave a comment below.
Happy coding! 🚀