Understanding the Composite API
Understanding the Composite API
The Salesforce Composite API is an advanced feature within Salesforce’s REST API framework. It allows developers to bundle multiple API requests into a single call. This feature significantly enhances the efficiency of data operations by reducing the number of network round-trips between a client application and Salesforce servers. The Composite API is capable of handling interdependent operations where the output of one request can serve as the input for another. This is particularly beneficial in complex integration scenarios or applications requiring efficient data exchange.
A single composite API call returns all responses and HTTP statuses for the subrequests in a unified response body. Additionally, the entire composite call counts as only one API call toward the organization's daily API call limits, making it highly resource-efficient.
Use Cases for the Composite API
The Composite API is ideal for situations requiring multiple operations to be executed in a specific sequence or involving interdependencies among those operations. For instance:
- Data Creation and Linking: Creating an account and immediately linking associated contacts or opportunities in a single API request.
- Batch Operations: Executing multiple CRUD (Create, Read, Update, Delete) operations in one go, reducing latency.
- Mobile and Web Applications: Optimizing operations for mobile or web applications where network performance and speed are critical.
- Integration with External Systems: Allowing external systems to interact with Salesforce in a consolidated and efficient manner.
These scenarios demonstrate the Composite API’s versatility in streamlining both simple and complex operations.
Inbound and Outbound Concepts
In the context of Salesforce APIs:
Inbound: Refers to external applications or systems making API calls to Salesforce to retrieve or modify data. The Composite API is primarily used in inbound scenarios. For example, a mobile app might need to create a customer record and related cases in Salesforce.
Outbound: Refers to Salesforce initiating API calls to external systems. This is typically handled by outbound messages, Apex callouts, or other tools, rather than the Composite API.
Supported Objects and Operations
The Composite API supports a wide range of standard and custom objects available in Salesforce. Here are the key points about its capabilities:
- Maximum Subrequests: A single composite request can include up to 25 subrequests.
- Query Operations: Among these 25 subrequests, a maximum of 5 can be query operations, such as SOQL queries (Query and QueryAll).
- API Version Requirement: Composite API operations are available in API versions 43.0 and later.
These supported features make it suitable for handling a wide variety of operations, though developers must be mindful of its constraints.
Advantages of Using the Composite API
The Composite API offers numerous advantages:
Efficiency in API Calls: By bundling multiple requests into one, the Composite API reduces the total number of API calls made. This is especially useful when an organization is close to its daily API call limit.
Improved Performance: Combining operations into a single request reduces network latency and enhances application performance. This is particularly beneficial for scenarios where multiple related operations need to be performed in real time.
Simplified Coding: The ability to bundle operations reduces the complexity of client-side code. Instead of making several independent API calls, developers can handle all operations in a single API call.
Atomic Transactions: The
allOrNone
flag provides transactional behavior. If one subrequest fails, the entire composite request can be rolled back, ensuring data integrity and consistency.Consolidated Responses: The API returns all subrequest responses in a single response body, simplifying error handling and debugging.
Limitations of the Composite API
While the Composite API is powerful, it has some limitations:
- Maximum Subrequests: Restricted to 25 subrequests per composite call.
- Query Limits: Only 5 subrequests can be query operations.
- Performance Considerations: Although it reduces network latency, the API can still face processing overhead on Salesforce’s end for large or complex composite requests.
- Error Handling: When using the
allOrNone
flag, a failure in any subrequest will cause the entire request to fail, which may not always be desirable. - API Version Dependency: Requires API version 43.0 or higher, limiting its usage in older environments.
Understanding these limitations is crucial for designing effective solutions with the Composite API.
Practical Example of Composite API Usage
Let’s explore a practical scenario where the Composite API is used to create an account and a related contact:
Request Body
{
"compositeRequest": [
{
"method": "POST",
"url": "/services/data/v56.0/sobjects/Account",
"referenceId": "newAccount",
"body": {
"Name": "Acme Corporation"
}
},
{
"method": "POST",
"url": "/services/data/v56.0/sobjects/Contact",
"referenceId": "newContact",
"body": {
"FirstName": "John",
"LastName": "Doe",
"AccountId": "@{newAccount.id}"
}
}
]
}
Explanation
- Account Creation: The first subrequest creates an account with the name “Acme Corporation” and assigns it a unique reference ID (
newAccount
). - Contact Creation: The second subrequest creates a contact named “John Doe” and links it to the account created in the first subrequest by referencing
newAccount.id
.
Response
The API returns a single consolidated response containing the results of both subrequests, including their HTTP statuses and data.
Conclusion
The Salesforce Composite API is a game-changer for developers seeking to optimize Salesforce integrations. By enabling multiple operations in a single call, it reduces complexity, enhances efficiency, and ensures a streamlined interaction with Salesforce data. Despite its limitations, when used strategically, the Composite API can significantly elevate the performance and maintainability of applications built on Salesforce.
Comments
Post a Comment