Hello Trailblazers,
In this post we're going to learn about Composite Resources in salesforce. Composite resources can improve your application's performance by minimizing round trips between client and salesforce (server). Let's try to learn by an example.
Get account details with related contacts and opportunities from Salesforce
- Get the account record using the account id by making a callout to standard Rest API
- Get the contact records related to the account record by making another callout to the standard Rest API
- Get the opportunity records related to the account record by making another callout to the standard Rest API
Composite API in Action - Get Records
{ "compositeRequest": [ { "method": "GET", "url": "/services/data/v55.0/sobjects/Account/0016D00000fHjSLQA0", "referenceId": "refAccount" }, { "method": "GET", "url": "/services/data/v55.0/sobjects/Account/@{refAccount.Id}/Contacts", "referenceId": "refContacts" }, { "method": "GET", "url": "/services/data/v55.0/sobjects/Account/@{refAccount.Id}/Opportunities", "referenceId": "refOpportunities" } ] }
- Get Account record using record id: Here, I've specified record id of the account record in the request.
- Get Contacts related to the current account: Here, you can also specify the account id itself in the request body but I've referenced the response of previous request and used it as an input here. Our previous request will return the account record in the request body which I'm referring as refAccount. That record will have an Id key in the response body with the value as record id of the account. So, I've specified @{refAccount.Id} in the URL of the second request where we're fetching the Contacts related to the account. We've specified refContacts as the reference id for this API response.
- Get Opportunities related to the current account: This is exact similar to the 2nd request, the only difference is - instead of getting contacts, here we're getting the Opportunities related to the current account record. The reference id for this request's response is refOpportunities.
{ "compositeResponse": [ { "body": { "attributes": { "type": "Account", "url": "/services/data/v55.0/sobjects/Account/0016D00000fHjSLQA0" }, "Id": "0016D00000fHjSLQA0", "IsDeleted": false, "MasterRecordId": null, "Name": "Sample Account", "Type": null, "ParentId": null, "BillingStreet": null, "BillingCity": null, "BillingState": null, "BillingPostalCode": null, "BillingCountry": null, "BillingLatitude": null, "BillingLongitude": null, "BillingGeocodeAccuracy": null, "BillingAddress": null, "ShippingStreet": null, "ShippingCity": null, "ShippingState": null, "ShippingPostalCode": null, "ShippingCountry": null, "ShippingLatitude": null, "ShippingLongitude": null, "ShippingGeocodeAccuracy": null, "ShippingAddress": null, "Phone": null, "Fax": null, "AccountNumber": null, "Website": null, "PhotoUrl": "/services/images/photo/0016D00000fHjSLQA0", "Sic": null, "Industry": null, "AnnualRevenue": null, "NumberOfEmployees": null, "Ownership": null, "TickerSymbol": null, "Description": null, "Rating": null, "Site": null, "OwnerId": "0056D000005v9kXQAQ", "CreatedDate": "2022-10-15T08:29:16.000+0000", "CreatedById": "0056D000005v9kXQAQ", "LastModifiedDate": "2022-10-15T08:29:16.000+0000", "LastModifiedById": "0056D000005v9kXQAQ", "SystemModstamp": "2022-10-15T08:29:16.000+0000", "LastActivityDate": null, "LastViewedDate": "2022-10-16T07:55:17.000+0000", "LastReferencedDate": "2022-10-16T07:55:17.000+0000", "Jigsaw": null, "JigsawCompanyId": null, "CleanStatus": "Pending", "AccountSource": null, "DunsNumber": null, "Tradestyle": null, "NaicsCode": null, "NaicsDesc": null, "YearStarted": null, "SicDesc": null, "DandbCompanyId": null, "OperatingHoursId": null }, "httpHeaders": { "ETag": "\"gTI7lF2oYMlDxM+gTW1a62nzqxfxxihRqAygUNh9DPs=\"", "Last-Modified": "Sat, 15 Oct 2022 08:29:16 GMT" }, "httpStatusCode": 200, "referenceId": "refAccount" }, { "body": { "totalSize": 2, "done": true, "records": [ { "attributes": { "type": "Contact", "url": "/services/data/v55.0/sobjects/Contact/0036D00000UAXTNQA5" }, "Id": "0036D00000UAXTNQA5", "IsDeleted": false, "MasterRecordId": null, "AccountId": "0016D00000fHjSLQA0", "LastName": "Contact 1", "FirstName": "Sample", "Salutation": "Mr.", "Name": "Sample Contact 1", "OtherStreet": null, "OtherCity": null, "OtherState": null, "OtherPostalCode": null, "OtherCountry": null, "OtherLatitude": null, "OtherLongitude": null, "OtherGeocodeAccuracy": null, "OtherAddress": null, "MailingStreet": null, "MailingCity": null, "MailingState": null, "MailingPostalCode": null, "MailingCountry": null, "MailingLatitude": null, "MailingLongitude": null, "MailingGeocodeAccuracy": null, "MailingAddress": null, "Phone": null, "Fax": null, "MobilePhone": null, "HomePhone": null, "OtherPhone": null, "AssistantPhone": null, "ReportsToId": null, "Email": null, "Title": null, "Department": null, "AssistantName": null, "LeadSource": null, "Birthdate": null, "Description": null, "OwnerId": "0056D000005v9kXQAQ", "CreatedDate": "2022-10-15T08:29:16.000+0000", "CreatedById": "0056D000005v9kXQAQ", "LastModifiedDate": "2022-10-16T07:46:44.000+0000", "LastModifiedById": "0056D000005v9kXQAQ", "SystemModstamp": "2022-10-16T07:46:44.000+0000", "LastActivityDate": null, "LastCURequestDate": null, "LastCUUpdateDate": null, "LastViewedDate": "2022-10-16T07:46:44.000+0000", "LastReferencedDate": "2022-10-16T07:46:44.000+0000", "EmailBouncedReason": null, "EmailBouncedDate": null, "IsEmailBounced": false, "PhotoUrl": "/services/images/photo/0036D00000UAXTNQA5", "Jigsaw": null, "JigsawContactId": null, "CleanStatus": "Pending", "IndividualId": null }, { "attributes": { "type": "Contact", "url": "/services/data/v55.0/sobjects/Contact/0036D00000ULNcUQAX" }, "Id": "0036D00000ULNcUQAX", "IsDeleted": false, "MasterRecordId": null, "AccountId": "0016D00000fHjSLQA0", "LastName": "Contact 2", "FirstName": "Sample", "Salutation": "Ms.", "Name": "Sample Contact 2", "OtherStreet": null, "OtherCity": null, "OtherState": null, "OtherPostalCode": null, "OtherCountry": null, "OtherLatitude": null, "OtherLongitude": null, "OtherGeocodeAccuracy": null, "OtherAddress": null, "MailingStreet": null, "MailingCity": null, "MailingState": null, "MailingPostalCode": null, "MailingCountry": null, "MailingLatitude": null, "MailingLongitude": null, "MailingGeocodeAccuracy": null, "MailingAddress": null, "Phone": null, "Fax": null, "MobilePhone": null, "HomePhone": null, "OtherPhone": null, "AssistantPhone": null, "ReportsToId": null, "Email": null, "Title": null, "Department": null, "AssistantName": null, "LeadSource": null, "Birthdate": null, "Description": null, "OwnerId": "0056D000005v9kXQAQ", "CreatedDate": "2022-10-16T07:46:25.000+0000", "CreatedById": "0056D000005v9kXQAQ", "LastModifiedDate": "2022-10-16T07:46:25.000+0000", "LastModifiedById": "0056D000005v9kXQAQ", "SystemModstamp": "2022-10-16T07:46:25.000+0000", "LastActivityDate": null, "LastCURequestDate": null, "LastCUUpdateDate": null, "LastViewedDate": "2022-10-16T07:46:26.000+0000", "LastReferencedDate": "2022-10-16T07:46:26.000+0000", "EmailBouncedReason": null, "EmailBouncedDate": null, "IsEmailBounced": false, "PhotoUrl": "/services/images/photo/0036D00000ULNcUQAX", "Jigsaw": null, "JigsawContactId": null, "CleanStatus": "Pending", "IndividualId": null } ] }, "httpHeaders": {}, "httpStatusCode": 200, "referenceId": "refContacts" }, { "body": { "totalSize": 2, "done": true, "records": [ { "attributes": { "type": "Opportunity", "url": "/services/data/v55.0/sobjects/Opportunity/0066D000005z3tpQAA" }, "Id": "0066D000005z3tpQAA", "IsDeleted": false, "AccountId": "0016D00000fHjSLQA0", "IsPrivate": false, "Name": "Sample Opportunity 1", "Description": null, "StageName": "Prospecting", "Amount": null, "Probability": 10.0, "ExpectedRevenue": null, "TotalOpportunityQuantity": null, "CloseDate": "2022-10-17", "Type": null, "NextStep": null, "LeadSource": null, "IsClosed": false, "IsWon": false, "ForecastCategory": "Pipeline", "ForecastCategoryName": "Pipeline", "CampaignId": null, "HasOpportunityLineItem": false, "Pricebook2Id": null, "OwnerId": "0056D000005v9kXQAQ", "CreatedDate": "2022-10-16T07:29:30.000+0000", "CreatedById": "0056D000005v9kXQAQ", "LastModifiedDate": "2022-10-16T07:47:09.000+0000", "LastModifiedById": "0056D000005v9kXQAQ", "SystemModstamp": "2022-10-16T07:47:09.000+0000", "LastActivityDate": null, "PushCount": 0, "LastStageChangeDate": null, "FiscalQuarter": 4, "FiscalYear": 2022, "Fiscal": "2022 4", "ContactId": null, "LastViewedDate": "2022-10-16T07:47:09.000+0000", "LastReferencedDate": "2022-10-16T07:47:09.000+0000", "HasOpenActivity": false, "HasOverdueTask": false, "LastAmountChangedHistoryId": null, "LastCloseDateChangedHistoryId": null }, { "attributes": { "type": "Opportunity", "url": "/services/data/v55.0/sobjects/Opportunity/0066D000005z3wPQAQ" }, "Id": "0066D000005z3wPQAQ", "IsDeleted": false, "AccountId": "0016D00000fHjSLQA0", "IsPrivate": false, "Name": "Sample Opportunity 2", "Description": null, "StageName": "Prospecting", "Amount": null, "Probability": 10.0, "ExpectedRevenue": null, "TotalOpportunityQuantity": null, "CloseDate": "2022-10-17", "Type": null, "NextStep": null, "LeadSource": null, "IsClosed": false, "IsWon": false, "ForecastCategory": "Pipeline", "ForecastCategoryName": "Pipeline", "CampaignId": null, "HasOpportunityLineItem": false, "Pricebook2Id": null, "OwnerId": "0056D000005v9kXQAQ", "CreatedDate": "2022-10-16T07:47:01.000+0000", "CreatedById": "0056D000005v9kXQAQ", "LastModifiedDate": "2022-10-16T07:47:01.000+0000", "LastModifiedById": "0056D000005v9kXQAQ", "SystemModstamp": "2022-10-16T07:47:01.000+0000", "LastActivityDate": null, "PushCount": 0, "LastStageChangeDate": null, "FiscalQuarter": 4, "FiscalYear": 2022, "Fiscal": "2022 4", "ContactId": null, "LastViewedDate": "2022-10-16T07:47:01.000+0000", "LastReferencedDate": "2022-10-16T07:47:01.000+0000", "HasOpenActivity": false, "HasOverdueTask": false, "LastAmountChangedHistoryId": null, "LastCloseDateChangedHistoryId": null } ] }, "httpHeaders": {}, "httpStatusCode": 200, "referenceId": "refOpportunities" } ] }
{ "method": "GET", "url": "/services/data/v55.0/sobjects/User/@{refAccount.OwnerId}", "referenceId": "refUser" }
Creating Records using Composite API
{ "compositeRequest": [ { "method": "POST", "url": "/services/data/v55.0/sobjects/Account", "referenceId": "refAccount", "body": { "Name": "My Sample Account" } }, { "method": "POST", "url": "/services/data/v55.0/sobjects/Contact", "referenceId": "refContact1", "body": { "FirstName": "My Sample", "LastName": "Contact 1", "AccountId": "@{refAccount.id}" } }, { "method": "POST", "url": "/services/data/v55.0/sobjects/Contact", "referenceId": "refContact2", "body": { "FirstName": "My Sample", "LastName": "Contact 2", "AccountId": "@{refAccount.id}" } }, { "method": "POST", "url": "/services/data/v55.0/sobjects/Opportunity", "referenceId": "refOpportunity1", "body": { "Name": "My Sample Opportunity 1", "AccountId": "@{refAccount.id}", "ContactId": "@{refContact1.id}", "StageName": "Prospecting", "CloseDate": "2022-10-20" } }, { "method": "POST", "url": "/services/data/v55.0/sobjects/Opportunity", "referenceId": "refOpportunity2", "body": { "Name": "My Sample Opportunity 2", "AccountId": "@{refAccount.id}", "ContactId": "@{refContact2.id}", "StageName": "Qualification", "CloseDate": "2022-10-20" } } ] }
{ "compositeResponse": [ { "body": { "id": "0016D00000fSq4HQAS", "success": true, "errors": [] }, "httpHeaders": { "Location": "/services/data/v55.0/sobjects/Account/0016D00000fSq4HQAS" }, "httpStatusCode": 201, "referenceId": "refAccount" }, { "body": { "id": "0036D00000ULNmoQAH", "success": true, "errors": [] }, "httpHeaders": { "Location": "/services/data/v55.0/sobjects/Contact/0036D00000ULNmoQAH" }, "httpStatusCode": 201, "referenceId": "refContact1" }, { "body": { "id": "0036D00000ULNmtQAH", "success": true, "errors": [] }, "httpHeaders": { "Location": "/services/data/v55.0/sobjects/Contact/0036D00000ULNmtQAH" }, "httpStatusCode": 201, "referenceId": "refContact2" }, { "body": { "id": "0066D000005z4LPQAY", "success": true, "errors": [] }, "httpHeaders": { "Location": "/services/data/v55.0/sobjects/Opportunity/0066D000005z4LPQAY" }, "httpStatusCode": 201, "referenceId": "refOpportunity1" }, { "body": { "id": "0066D000005z4LQQAY", "success": true, "errors": [] }, "httpHeaders": { "Location": "/services/data/v55.0/sobjects/Opportunity/0066D000005z4LQQAY" }, "httpStatusCode": 201, "referenceId": "refOpportunity2" } ] }
Bonus Content: All or None
{ "compositeRequest": [ { "method": "POST", "url": "/services/data/v55.0/sobjects/Account", "referenceId": "refAccount", "body": { "Name": "My Sample Account" } }, { "method": "POST", "url": "/services/data/v55.0/sobjects/Contact", "referenceId": "refContact1", "body": { "FirstName": "My Sample", "LastName": "Contact 1", "AccountId": "@{refAccount.id}" } }, { "method": "POST", "url": "/services/data/v55.0/sobjects/Contact", "referenceId": "refContact2", "body": { "FirstName": "My Sample", "LastName": "Contact 2", "AccountId": "@{refAccount.id}" } }, { "method": "POST", "url": "/services/data/v55.0/sobjects/Opportunity", "referenceId": "refOpportunity1", "body": { "Name": "My Sample Opportunity 1", "AccountId": "@{refAccount.id}", "ContactId": "@{refContact1.id}", "StageName": "Prospecting", "CloseDate": "2022-10-20" } }, { "method": "POST", "url": "/services/data/v55.0/sobjects/Opportunity", "referenceId": "refOpportunity2", "body": { "Name": "My Sample Opportunity 2", "AccountId": "@{refAccount.id}", "ContactId": "@{refContact2.id}", "CloseDate": "2022-10-20" } } ], "allOrNone": true }
{ "compositeResponse": [ { "body": [ { "errorCode": "PROCESSING_HALTED", "message": "The transaction was rolled back since another operation in the same transaction failed." } ], "httpHeaders": {}, "httpStatusCode": 400, "referenceId": "refAccount" }, { "body": [ { "errorCode": "PROCESSING_HALTED", "message": "The transaction was rolled back since another operation in the same transaction failed." } ], "httpHeaders": {}, "httpStatusCode": 400, "referenceId": "refContact1" }, { "body": [ { "errorCode": "PROCESSING_HALTED", "message": "The transaction was rolled back since another operation in the same transaction failed." } ], "httpHeaders": {}, "httpStatusCode": 400, "referenceId": "refContact2" }, { "body": [ { "errorCode": "PROCESSING_HALTED", "message": "The transaction was rolled back since another operation in the same transaction failed." } ], "httpHeaders": {}, "httpStatusCode": 400, "referenceId": "refOpportunity1" }, { "body": [ { "message": "Required fields are missing: [StageName]", "errorCode": "REQUIRED_FIELD_MISSING", "fields": [ "StageName" ] } ], "httpHeaders": {}, "httpStatusCode": 400, "referenceId": "refOpportunity2" } ] }
No comments:
Post a Comment