Blog
API Simplified

Introduction to Salesforce Composite API.

Praveen Chandran R
07 Feb 2025
5 min read

Introduction to Salesforce Composite API

I’m excited to introduce you to the Salesforce Composite API, a powerful tool designed to streamline our API interactions. By allowing us to perform multiple API requests in a single call, the Composite API not only accelerates our workflows but also ensures data consistency and smooth transactions. Whether you're creating, updating, or deleting related records, the Composite API has got you covered. Let's dive into some detailed examples to see how it works in practice.

Example 1: Creating Nested Records

Imagine you need to create an account and simultaneously add a related contact and case. Normally, this would require separate API calls for each record, but with the Composite API, you can handle it all in one request.

                    {
                      "compositeRequest": [
                        {
                          "method": "POST",
                          "url": "/services/data/v62.0/sobjects/Account",
                          "referenceId": "NewAccount",
                          "body": {
                            "Name": "Nested Account"
                          }
                        },
                        {
                          "method": "POST",
                          "url": "/services/data/v62.0/sobjects/Contact",
                          "referenceId": "NewContact",
                          "body": {
                            "FirstName": "prav",
                            "LastName": "een",
                            "AccountId": "@{NewAccount.id}"
                          }
                        },
                        {
                          "method": "POST",
                          "url": "/services/data/v62.0/sobjects/Case",
                          "referenceId": "NewCase",
                          "body": {
                            "Subject": "New Case for Nested Account",
                            "AccountId": "@{NewAccount.id}",
                            "ContactId": "@{NewContact.id}"
                          }
                        }
                      ]
                    }
                  

Example 2: Deleting Records with All-or-None

Next, let’s say you need to delete multiple records like an account, a contact, and an opportunity. The allOrNone parameter ensures that either all deletions happen, or none do, maintaining data integrity.

                    {
                      "allOrNone": true,
                      "compositeRequest": [
                        {
                          "method": "DELETE",
                          "url": "/services/data/v62.0/sobjects/Account/001dM00002JS9fZQAT",
                          "referenceId": "DeleteAccount"
                        },
                        {
                          "method": "DELETE",
                          "url": "/services/data/v62.0/sobjects/Contact/003XXXXXXXXXXXXXXX",
                          "referenceId": "DeleteContact"
                        },
                        {
                          "method": "DELETE",
                          "url": "/services/data/v62.0/sobjects/Opportunity/006XXXXXXXXXXXXXXX",
                          "referenceId": "DeleteOpportunity"
                        }
                      ]
                    }
                  

Example 3: Updating Multiple Records

Now, let’s look at how to update multiple records in one call. We’ll update the phone number and website of an account, and the phone number and email of a contact.

                    {
                      "compositeRequest": [
                        {
                          "method": "PATCH",
                          "url": "/services/data/v62.0/sobjects/Account/{AccountId}",
                          "referenceId": "UpdateAccount",
                          "body": {
                            "Phone": "987-654-3210",
                            "Website": "https://www.updatedweb.com"
                          }
                        },
                        {
                          "method": "PATCH",
                          "url": "/services/data/v62.0/sobjects/Contact/{ContactId}",
                          "referenceId": "UpdateContact",
                          "body": {
                            "Phone": "987-654-3210",
                            "Email": "updatedemail@twst.com"
                          }
                        }
                      ]
                    }
                  

Example 4: Updating Records with Specific IDs

Lastly, let’s update records using specific IDs. This example is similar to the previous one but uses actual record IDs.

                    {
                      "compositeRequest": [
                        {
                          "method": "PATCH",
                          "url": "/services/data/v62.0/sobjects/Account/001XXXXXXXXXXXXXXX",
                          "referenceId": "UpdateAccount",
                          "body": {
                            "Phone": "987-654-3210",
                            "Website": "https://www.updated-website.com"
                          }
                        },
                        {
                          "method": "PATCH",
                          "url": "/services/data/v62.0/sobjects/Contact/003XXXXXXXXXXXXXXX",
                          "referenceId": "UpdateContact",
                          "body": {
                            "Phone": "987-654-3210",
                            "Email": "updated-email@demo.com"
                          }
                        }
                      ]
                    }
                  

Example 5: Retrieving Related Records

In this example, we will retrieve an account and its related contacts and opportunities.

                    {
                      "compositeRequest": [
                        {
                          "method": "GET",
                          "url": "/services/data/v62.0/sobjects/Account/001dM00002JS9fZQAT",
                          "referenceId": "AccountInfo"
                        },
                        {
                          "method": "GET",
                          "url": "/services/data/v62.0/query/?q=SELECT+Id,FirstName,LastName,Email,Phone+FROM+Contact+WHERE+AccountId='@{AccountInfo.id}'",
                          "referenceId": "AccountContacts"
                        },
                        {
                          "method": "GET",
                          "url": "/services/data/v62.0/query/?q=SELECT+Id,Name,StageName,CloseDate,Amount+FROM+Opportunity+WHERE+AccountId='@{AccountInfo.id}'",
                          "referenceId": "AccountOpportunities"
                        }
                      ]
                    }
                  

Conclusion

In conclusion, the Salesforce Composite API is a versatile tool that allows you to perform multiple operations in a single API call. Whether you are creating nested records, deleting multiple records with the all-or-none flag, or updating multiple records, the Composite API makes the process efficient and consistent.

I hope these detailed examples have given you a clear understanding of the Composite API's capabilities and how you can use it to simplify your work in Salesforce.

Share this post
FEATURED BLog

Join Us on Our Learning Journey

We are a team dedicated to continuous learning. Every Week, we explore new ideas, gain fresh insights, and turn our discoveries into valuable blog posts. Follow along as we grow, share, and evolve—one lesson at a time!

By clicking Subscribe Up you're confirming that you agree with our Terms and Conditions.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.