Composite API is a feature of Salesforce's REST API that allows you to execute multiple API requests in a single call. It helps you do multiple operations, like read, create, update, and delete Salesforce data, in a single callout.
The output of the initial request can be used as the input for a subsequent request. The Composite resource is an enhanced form of the REST API that executes a series of REST API requests in a single call. The Composite Request Body describes a collection of subrequests to execute with the Composite Resource.
Using the Composite API to create a Lead with multiple documents
To create a composite request body, you need to provide the following information:
An allOrNone flag that specifies how to roll back errors
A collateSubrequests flag controls whether the API collates unrelated subrequests to bulkify them
A compositeRequest collection that includes subrequests to execute
Each subrequest contains the resource, method, headers, body, and reference ID for the subrequest. The body type depends on the request specified in the URL property. The reference ID can be used to refer to the output of one subrequest as the input of another subrequest.
Example:
{ "allOrNone": true,
"collateSubrequests": true,
"compositeRequest":[
{
"method":"POST",
"url":"/services/data/v56.0/sobjects/Lead",
"referenceId":"refLead",
"body":{
"FirstName":"Test",
"LastName":"User",
"RecordTypeId":"0121y000002OULE"
}
},
{
"method":"POST",
"url":"/services/data/v56.0/sobjects/ContentVersion",
"referenceId":"refcv0",
"body":{
"PathOnClient":"Hello.txt",
"VersionData":"SGVsbG8gV29ybGQx"
}
},
{
"method":"GET",
"url":"/services/data/v56.0/sobjects/ContentVersion/@{refcv0.id}",
"referenceId":"refcd0"
},
{
"method":"POST",
"url":"/services/data/v56.0/sobjects/ContentDocumentLink",
"referenceId":"refcdl0",
"body":{
"LinkedEntityId":"@{refLead.id}",
"ContentDocumentId":"@{refcd0.ContentDocumentId}",
"ShareType":"V"
}
},
{
"method":"POST",
"url":"/services/data/v56.0/sobjects/ContentVersion",
"referenceId":"refcv1",
"body":{
"PathOnClient":"Hello2.txt",
"VersionData":"SGVsbG8gV29ybGQy"
}
},
{
"method":"GET",
"url":"/services/data/v56.0/sobjects/ContentVersion/@{refcv1.id}",
"referenceId":"refcd1"
},
{
"method":"POST",
"url":"/services/data/v56.0/sobjects/ContentDocumentLink",
"referenceId":"refcdl1",
"body":{
"LinkedEntityId":"@{refLead.id}",
"ContentDocumentId":"@{refcd1.ContentDocumentId}",
"ShareType":"V"
}
}
]
}
This is an example of a composite request in Salesforce. It contains an array of subrequests that will be executed together in a single composite request. Each subrequest contains the HTTP method, URL, reference ID, and body.
You can see this in the image below. The lead has been created, and attachments are linked to the lead record.
Explanation of each subrequest:
The first subrequest is a POST request that creates a Lead object with the first name, last name, and record type ID provided in the body.
The second subrequest is also a POST request that creates a ContentVersion object with a file name and base64-encoded file content provided in the body.
The third subrequest is a GET request that retrieves the ContentVersion object created in the second subrequest by referencing its ID.
The fourth subrequest is a POST request that creates a ContentDocumentLink object that links the Lead object created in the first subrequest to the ContentVersion object retrieved in the third subrequest.
The fifth and sixth subrequests are similar to the second and fourth subrequests, respectively, but they create a different ContentVersion object and link it to the same Lead object.
Overall, this Composite API request creates a new Lead record, uploads two files and links them to the Lead record, and grants view access to those files to the Lead record.
Happy!! to help you add to your knowledge. You can leave a comment to help me understand how the blog helped you. If you need further assistance, please contact us. You can click "Reach Us" on the website and share the issue with me.
Blog Credit:
Z. G. Mir
Salesforce Developer
Avenoir Technologies Pvt. Ltd.
Reach us: team@avenoir.ai
댓글