
If you are looking for a way to optimize your REST API calls to Salesforce, you might want to check out the composite resource. The composite resource allows you to execute multiple REST API requests in a single call, saving you time and bandwidth. In this blog post, I will explain what the composite resource is, how it works, and show you some examples of how to use it.
What is the composite resource?
The composite resource is a special type of REST API resource that lets you combine multiple requests into one. You can use it to perform CRUD operations on multiple records, retrieve data from multiple objects, or execute complex logic that involves multiple steps. The composite resource has two subresources: composite and composite.
The composite subresource executes a series of REST API requests in a single call. The output of one request can be used as the input of another request using reference IDs. The response bodies and HTTP statuses of the requests are returned in a single response body. The composite subresource supports up to 25 subrequests per call.
The composite subresource executes a series of CRUD operations on a large number of related sObjects in a single call. The subrequests are organized into graphs, each representing a collection of sObjects with relationships. The composite subresource handles the orchestration of the relationships and ensures that the records are processed efficiently and atomically. The composite subresource supports up to 500 subrequests per call.
How does the composite resource work?
To use the composite resource, you need to send a POST request to the /services/data/vXX.X/composite endpoint, where XX.X is the API version. The request body should contain an array of subrequests, each specifying the method, url, referenceId, and body (if applicable) of the individual REST API request. For example, here is a simple composite request that creates an account and a contact:
{ | |
"compositeRequest": [ | |
{ | |
"method": "POST", | |
"url": "/services/data/v52.0/sobjects/Account", | |
"referenceId": "newAccount", | |
"body": { | |
"Name": "Acme Inc." | |
} | |
}, | |
{ | |
"method": "POST", | |
"url": "/services/data/v52.0/sobjects/Contact", | |
"referenceId": "newContact", | |
"body": { | |
"LastName": "Smith", | |
"FirstName": "John", | |
"AccountId": "@{newAccount.id}" | |
} | |
} | |
] | |
} |
The response body will contain an array of results, each corresponding to the subrequest in the same order. Each result will have a referenceId, httpStatusCode, and body (if applicable) fields. For example, here is the response for the previous request:
{ | |
"compositeResponse": [ | |
{ | |
"referenceId": "newAccount", | |
"httpStatusCode": 201, | |
"body": { | |
"id": "0013h00000ZlZ4FAAV", | |
"success": true, | |
"errors": [] | |
} | |
}, | |
{ | |
"referenceId": "newContact", | |
"httpStatusCode": 201, | |
"body": { | |
"id": "0033h00000ZlZ4FAAV", | |
"success": true, | |
"errors": [] | |
} | |
} | |
] | |
} |
As you can see, the referenceId newAccount was used to link the account and contact records. You can use reference IDs to access any field of the previous subrequest using the syntax @{referenceId.fieldName}. You can also use dot notation to access nested fields, such as @{referenceId.body.id}.
What are some use cases for the composite resource?
The composite resource can be useful for many scenarios where you need to perform multiple REST API requests in a single call. Here are some examples:
- Bulk data loading: You can use the composite resource to insert, update, or delete multiple records in one call, reducing the number of API calls and improving performance.
- Data synchronization: You can use the composite resource to synchronize data between Salesforce and an external system, such as a mobile app or a web service. You can use reference IDs to link records that depend on each other, such as parent-child relationships.
- Complex logic: You can use the composite resource to execute complex logic that involves multiple steps, such as creating a case, assigning it to a queue, and sending an email notification. You can use reference IDs to pass data between subrequests, such as case ID or email address.
Advantages of Salesforce Composite API:
- Improved Performance: The Composite API minimizes the number of network requests, resulting in reduced latency and improved overall performance. By grouping multiple operations in a single request, you can achieve faster data processing and save valuable server resources.
- Atomicity and Consistency: The Composite API ensures atomicity, meaning that either all operations within a request succeed or none of them are performed. This guarantees data consistency and integrity, preventing partial updates or discrepancies in your Salesforce database.
- Enhanced Developer Productivity: With the Composite API, developers can accomplish more with fewer API calls. This streamlined approach simplifies the coding process, reduces complexity, and improves development efficiency, allowing you to focus on building robust applications.
- Reduced API Consumption: Since the Composite API enables you to perform multiple operations within a single request, you can significantly reduce the number of API calls, which contributes to cost optimization, particularly for organizations with high API consumption requirements.
- Transaction Control: Salesforce Composite API provides transactional control, allowing you to execute all operations within a request as a single transaction. This means that you can roll back changes if an error occurs during the transaction, maintaining data integrity and consistency
Best Practices for Utilizing Salesforce Composite API:
- Batch Operations: When using the Composite API, make sure to group similar operations together, such as creating or updating multiple records of the same object. This approach minimizes the number of API requests and improves efficiency.
- Error Handling: Implement robust error handling mechanisms to capture and handle errors effectively. The Composite API response includes sub-responses for each operation, allowing you to identify and handle errors at an individual operation level.
- Governor Limits Consideration: While the Composite API optimizes API consumption, it’s essential to keep Salesforce’s governor limits in mind. Stay informed about the limits for different types of operations and design your requests accordingly to avoid hitting those limits.
- Performance Optimization: Optimize your requests by carefully selecting the fields to retrieve, reducing unnecessary data transfer. Additionally, consider asynchronous processing for operations that don’t require immediate responses, further improving performance.
- Throttling and Backoff Strategy: Be mindful of Salesforce’s API rate limits to prevent throttling. Implement a backoff strategy to handle cases when API limits are reached, allowing your application to automatically retry after a certain period.
Conclusion
The composite resource is a powerful tool that allows you to optimize your REST API calls to Salesforce. It lets you execute multiple requests in a single call, saving you time and bandwidth. It also handles the orchestration of related records and ensures atomicity of operations. You can use the composite resource to perform CRUD operations on multiple records, retrieve data from multiple objects, or execute complex logic that involves multiple steps.
I hope you found this blog post helpful and informative. If you have any questions or feedback, please leave a comment below. Thank you for reading!
About the blog
SFDCLessons is a blog where you can find various Salesforce tutorials and tips that we have written to help beginners and experienced developers alike. we also share my experience and knowledge on Salesforce best practices, troubleshooting, and optimization. Don’t forget to follow us on:
Newsletter
Subscribe to our email newsletter to be notified when a new post is published.