Tip of the week # 216 : Salesforce Composite and Composite Tree API



Salesforce Composite and Composite Tree API with Examples

Description : Executes a series of REST API requests in a single POST request, or retrieves a list of other composite resources with a GET request.
Salesforce REST Composite API's :
  • Salesforce REST Composite : To perform multiple REST API calls in a single request.Batch multiple API sub-requests together, execute sequentially (or in parallel), and optionally reference results between them.

    
        
         Method:- Post (For Insert)
         URL:-/services/data/v64.0/composite
         Request Body :-<br>
         {
          "compositeRequest": [
            {
              "method": "POST",
              "url": "/services/data/v64.0/sobjects/Account",
              "referenceId": "newAccount",
              "body": { "Name": "Test Account" }
            },
            {
              "method": "POST",
              "url": "/services/data/v64.0/sobjects/Contact",
              "referenceId": "newContact",
              "body": { "LastName": "Smith", "AccountId": "@{newAccount.id}" }
            }
          ]
        }
    
        
    	
    	
    
        
         Method:- Post (For Update and Delete)
         URL:-/services/data/v64.0/composite
         Request Body :-<br>
         {
          "compositeRequest": [
            {
              "method": "Delete",
              "url": "/services/data/v58.0/sobjects/Account/a00000000000001AAA",
              "referenceId": "newAccountDel",
              "body": { "Name": "Test Account" }
            },
            {
               "method" : "PATCH",
               "url" : "/services/data/v64.0/sobjects/Account/a00000000000001ABC",
               "referenceID" : "NewAccountUpdate",
               "body" : { "Name" : "Update Acme" }
            }
          ]
        }
    
        
    	
    	
    Note : 1. GET, POST, PATCH, DELETE
    2. Up to 25 sub-requests in one call
    3. Can reference response values of earlier sub-requests using @{referenceId.id}
    4. When you want to insert/update/read/delete multiple unrelated records in a single round trip.
    5. You can also used External Id for DML Operations
  • Composite Tree API: To create/update a hierarchy of related records in a single request.Insert records that have parent-child relationships (like Account → Contact → Opportunity) in one go.

    
        
        	Method:- Post
            URL:- /services/data/v64.0/tree
            Request Body :-<br>
    		{
            "records": [
              {
                "attributes": { "type": "Account", "referenceId": "ref1" },
                "Name": "Tech Corp",
                "Contacts": {
                  "records": [
                    {
                      "attributes": { "type": "Contact", "referenceId": "ref2" },
                      "LastName": "Smith",
                      "Email": "smith@techcorp.com"
                    },
                    {
                      "attributes": { "type": "Opportunity", "referenceId": "ref3" },
                      "Name": "Johnson Opportunity"
                    }
                  ]
                }
              }
            ]
          }
    
        
    	
    	
    Note : 1. Up to 200 records in a single request
    2. Only works with sObject trees (cannot do GET or DELETE)
    3. Supports relationships via referenceId in the request body
    4. When you want to insert a nested record structure (like Account with its Contacts, Opportunities, and related records).
  • Note : you can monitor the job with below REST API /services/data/v64.0/jobs/ingest/7505fEXAMPLE4C2AAM(Job Id)/successfulResults/

Happy Sharing...

Everyone has their own favorites, so please feel free to share with yours contacts and comments below for any type of help!

Use below Chrome Extension for Productivity and share your feedback. Download Link : https://chromewebstore.google.com/detail/quick-shortcuts/ldodfklenhficdgllchmlcldbpeidifp?utm_source=ext_app_menu

Comments

Popular posts from this blog

User Data Privacy

Salesforce LWC : Compact Layout on Hover

Salesforce : Multi-Factor Authentication