Conquering Complexity: A Deep Dive into the Salesforce Composite API

Salesforce, the king of the CRM cloud, empowers businesses with vast data and functionality. But sometimes, navigating this treasure trove of information can feel like traversing a dense rainforest – full of potential but also overwhelming and complex. Enter the Salesforce Composite API, your machete for hacking through the undergrowth and accessing the precise data you need with elegance and efficiency.

What is the Salesforce Composite API?

Think of the Composite API as a powerful tool for combining multiple Salesforce API calls into a single, streamlined request. Instead of making numerous individual calls and stitching the results together, you can now orchestrate a symphony of data retrieval in one fell swoop.

Why Use the Composite API?

Here's why the Composite API might be your new favorite Salesforce companion:

  • Batch Processing: The Composite API supports batch processing, enabling developers to group multiple requests into a single call. This minimizes the number of API calls and improves the speed of data retrieval and manipulation.

  • Atomic Transactions: Atomic transactions ensure that either all operations within a composite request are executed successfully or none of them are. This compliance with ACID (Atomicity, Consistency, Isolation, Durability) ensures data integrity and consistency, even during failures.

  • Chaining Requests: Developers can chain requests within a composite call, allowing the output of one operation to serve as input for another. This sequential execution of requests simplifies complex workflows and dependencies.

  • Parallel Processing: The Composite API supports parallel processing, allowing multiple independent operations to be executed simultaneously. This feature enhances performance by leveraging the full potential of Salesforce's infrastructure.

Use Cases

  1. Bulk Data Manipulation: The Composite API is useful for bulk data manipulation scenarios. Developers can create composite requests to insert, update, or delete large volumes of records in a single call, reducing the load on the system.

  2. Complex Workflows: Organizations with intricate business processes can benefit from the Composite API's ability to handle complex workflows. Chaining requests and executing them atomically ensures that critical business operations are carried out seamlessly.

  3. Optimized Performance: The Composite API proves to be a valuable asset for applications demanding high performance and minimal latency. Minimizing the number of API calls required significantly improves the overall responsiveness of Salesforce integrations.

Understanding the Structure

A Composite API request resembles a well-defined recipe. It consists of:

  • Operations: Individual API calls, like querying accounts or updating opportunities.

  • Variables: Placeholders for values that get substituted during execution.

  • Dependencies: Define the order in which operations should be executed.

{
  "compositeRequest": [
    {
      "method": "HTTP_METHOD",  // e.g., GET, POST, PATCH, DELETE
      "url": "/services/data/vXX.X/sobjects/OBJECT_NAME",
      "referenceId": "REFERENCE_ID",  // Optional, for referencing later
      "body": {  // Request body, if applicable
        // Data for the operation
      }
    },
    // Additional operations...
  ]
}
  • Variables: Use @{variableName} to reference data from previous operations.

  • Dependencies: Specify "dependencies": ["referenceId1", "referenceId2"] to control execution order.

  • Nested Requests: Create complex data flows by nesting composite requests within operations.

  • Conditional Branching: Use "if": { "condition": "EXPRESSION" } to execute operations conditionally.

  • Error Handling: Set "faultTolerant": true for overall requests or "httpStatusCode": 400 specific operations.

Unleashing the Power

Let us create a new account and its associated contact in a single request.

{
  "compositeRequest": [
    {
      "method": "POST",
      "url": "/services/data/v55.0/sobjects/Account",
      "referenceId": "createAccount",
      "body": {
        "Name": "New Account from Composite API"
      }
    },
    {
      "method": "POST",
      "url": "/services/data/v55.0/sobjects/Contact",
      "referenceId": "createContact",
      "body": {
        "LastName": "Composite",
        "AccountId": "@{createAccount.id}"  // Reference account ID from previous operation
      }
    }
  ]
}

Explanation:

  1. Structure:

    • The request contains two operations within the compositeRequest array.

    • Each operation has a method, url, referenceId, and body.

    • referenceId Allows referencing data from previous operations.

  2. Operation 1:

    • Creates a new account using the POST method on the Accounts endpoint.

    • Stores the created account's ID in the createAccount.id variable.

  3. Operation 2:

    • Creates a new contact using the POST method on the Contacts endpoint.

    • Uses the @{createAccount.id} variable to link the contact to the newly created account.

Best Practices

  1. Optimize Payloads: Efficient use of the Composite API involves optimizing payloads to include only necessary data. This not only reduces the size of requests but also enhances performance.

  2. Error Handling: Implement robust error handling mechanisms to gracefully manage situations where one or more operations within a composite request fail. This ensures that failures do not compromise the integrity of the entire transaction.

Beyond the Basics

The Composite API offers advanced features to tackle even the most intricate data scenarios. To build sophisticated data flows, you can leverage nested requests, conditional branching, and error handling.

Ready to Embrace the Composite API?

Whether you're a seasoned Salesforce developer or just starting your journey, the Composite API promises a smoother, more efficient way to interact with your data. Embrace this powerful tool, confidently navigate the Salesforce rainforest, and unlock your CRM's full potential.

Additional Resources

Did you find this article valuable?

Support Treetop's team blog by becoming a sponsor. Any amount is appreciated!