Using Composite Graphs in Salesforce

SoomjeetSahoo
4 min readMay 8, 2022
Composite Resource

Businesses are improving and increasing their footprint at scale, thanks to globalization and ease of trading. But this also brings us another business challenge to deal with - Large Data Volumes and integrations, to make sure these data end up on the right systems which again needs to be managed efficiently. Composite Resources is one such tool to make data creation in salesforce easier.

Composite Resources, an upgrade to standard salesforce rest API, which was used traditionally and is still being used for invoking create and update operations via APIs from external systems.

You might ask why composite resources now? Why did salesforce go with this enhancement over the traditional approach? Let’s say, you want to update an account and create a contact as well. The traditional API approach will require 2 different calls - One for updating the Account and the other for creating a contact.

I urge you to go through this article from apexhours, which was the base for all my study on composite resources. With that said, I’ll keep this article short and to the point of implementing Composite Graph APIs with a Use-Case.

Before diving into the business scenario, first let’s have a look at what the request body for a composite resource should look like:

{
"allOrNone" : true,
"collateSubrequests": true,
"compositeRequest" : [{
Composite Subrequest
},{
Composite Subrequest
},{
Composite Subrequest
}]
}

allOrNone :
An Attribute which, when true, rolls back all the requests if one of the composite subrequests gets a failure.

collateSubrequests :
An Attribute which, when true, collects and executes all the non-related composite subrequest in parallel mode. In API version 49.0 and later, the default value is true. In version 48.0, the default value is false.

Business Scenario:

Rover Consulting(RC), a trusted customer for Universal Containers(UC), wants to set up its presence in the APAC Region. After RC submits the web form with details for processing on the UC’s website, the following things need to be created.

  1. An Account specific to RC’s APAC division, child to main RC Account.
  2. A Primary Contact for RC’s APAC Region.
  3. A Campaign record for the APAC Region.
  4. An Opportunity for the RC’s APAC division and the above campaign needs to be associated with it.
  5. A Default task for the opportunity.

Few assumptions before implementing:

  • I assumed each and every account to have an external id since it is created from an external system, which will be useful for associating the child account to the parent.
  • The task is created & is assigned to the user who is making the rest call.
  • I am using Postman to make callouts.

Let’s build our whole request body part by part :

  1. Composite subrequest for creating the child account.
/*Getting the parent account to associate the child account under it*/
{
"method": "GET",
"referenceId": "parent_account",
"url": "/services/data/v53.0/sobjects/Account/External_Id__c/fc512fac-d3e0-441a-9c32-d226e1581694"
},
/*Child Account created with parameters in body and is associated with parent Account*/
{
"url": "/services/data/v53.0/sobjects/Account/",
"body": {
"name": "@{parent_account.Name} APAC",
"description": "Child account For @{parent_account.Name} in APAC Region",
"Phone": "@{parent_account.Phone}",
"External_Id__c":"893d0807-4d17-4a55-bee1-2c7fae0ac3f6",
"ParentId": "@{parent_account.Id}"
},
"method": "POST",
"referenceId": "child_account"
}

2. Composite subrequest for creating Primary Contact for RC’s APAC Region.

{
"url": "/services/data/v53.0/sobjects/Contact/",
"body": {
"FirstName": "@{parent_account.Name} APAC",
"LastName": "Primary Contact",
"Phone": "@{parent_account.Phone}",
"AccountId": "@{child_account.id}"
},
"method": "POST",
"referenceId": "primary_contact_under_child_account"
}

3. Composite subrequest for creating campaign records for the APAC Region.

{
"url": "/services/data/v53.0/sobjects/Campaign/",
"body": {
"name": "APAC Region Campaign"
},
"method": "POST",
"referenceId": "primary_campaign"
}

4. Composite subrequest for creating an opportunity for the RC’s APAC division and associating the campaign with it.

{
"url": "/services/data/v53.0/sobjects/Opportunity/",
"body": {
"name": "@{parent_account.Name} APAC Opportunity",
"stageName": "Value Proposition",
"closeDate": "2025-12-31",
"CampaignId": "@{primary_campaign.id}",
"AccountId": "@{child_account.id}"
},
"method": "POST",
"referenceId": "primary_opportunity"
}

5. Composite subrequest for creating a default task for the opportunity.

{
"url": "/services/data/v53.0/sobjects/Task/",
"body": {
"WhatId": "@{primary_opportunity.id}",
"WhoId": "@{primary_contact_under_child_account.id}",
"Subject": "Call",
"Status": "Not Started",
"Description": "Default Task Opportunity"
},
"method": "POST",
"referenceId": "default_task_call"
}

Finally the whole body should look like below:

Callout Structure:

  • Endpoint : https://<Instance>.salesforce.com/services/data/v53.0/composite/graph
  • Headers: “ Authorization” : “Bearer <accessToken>”
  • Body: <Composite JSON above>

P.S. Make sure you use the same API version across both endpoint URL and service URL in all the nodes.

OnScreen Demo

I hope this short article helps you out on implementing composite resources. As always I urge you guys to have a look at this article from apexhours for more info.

--

--

SoomjeetSahoo

Salesforce Dev @ Salesforce with lots of inquisitiveness to learn and grow in Salesforce and Node Domains. Currently 15x Salesforce Certified & 14x Super Badge.