Sunday, February 27, 2022

Salesforce Composite Connector

Executes a series of REST API requests in a single call. You can use the output of one request as the input to a subsequent request. The response bodies and HTTP statuses of the requests are returned in a single response body. The entire series of requests counts as a single call toward your API limits.


The requests in a composite call are called subrequests. All subrequests are executed in the context of the same user. In a subrequest’s body, you specify a reference ID that maps to the subrequest’s response. You can then refer to the ID in the url or body fields of later subrequests by using a JavaScript-like reference notation.

For example, the following composite request body includes two subrequests. The first creates an account and designates the output as refAccount. The second creates a contact parented under the new account by referencing refAccount in the subrequest body.

{
"compositeRequest" : [{
  "method" : "POST",
  "url" : "/services/data/v54.0/sobjects/Account",
  "referenceId" : "refAccount",
  "body" : { "Name" : "Sample Account" }
  },{
  "method" : "POST",
  "url" : "/services/data/v54.0/sobjects/Contact",
  "referenceId" : "refContact",
  "body" : { 
    "LastName" : "Sample Contact",
    "AccountId" : "@{refAccount.id}"
    }
  }]
}

Step1:

For Consumer key and secret, we need to create a connected app in Salesforce.

Go to Setup -> Build -> Create -> Apps -> Create new in Connected apps


Step2:
 Enable OAuth setting:

Step3:
Save the app.
Step4:
Click on Manage and set Permitted Users as All users may self-authorize, IP Relaxation as Relax IP restrictions.


Step5:
Now get the Consumer id and secret from the custom app:        
 

Step6:
Anypoint Studio: Create your API..(refer Github link)

Use this same key and secret in the Salesforce Composite Connector Configuration:

Sample flow:



Test Data 1:

{
"compositeRequest" : [
  {
  "method" : "POST",
  "url" : "/services/data/v51.0/sobjects/Account",
  "referenceId" : "refAccount",
  "body" : { "Name" : "James Bond  007" }
  },
  {
  "method" : "POST",
  "url" : "/services/data/v51.0/sobjects/Contact",
  "referenceId" : "refContact",
  "body" : { 
    "LastName" : "David Ross",
    "AccountId" : "@{refAccount.id}"
    }
  }
  ]
}

Test Data 2:
{
"compositeRequest" : [
  {
  "method" : "POST",
  "url" : "/services/data/v51.0/sobjects/Account",
  "referenceId" : "refAccount1",
  "body" : { "Name" : "Michael 1" }
  },
  {
  "method" : "POST",
  "url" : "/services/data/v51.0/sobjects/Account",
  "referenceId" : "refAccount2",
  "body" : { "Name" : "Michael 2" }
  },
  {
  "method" : "POST",
  "url" : "/services/data/v51.0/sobjects/Contact",
  "referenceId" : "refContact1",
  "body" : { 
    "LastName" : "Ross 1",
    "AccountId" : "@{refAccount1.id}"
    }
  },
  {
  "method" : "POST",
  "url" : "/services/data/v51.0/sobjects/Contact",
  "referenceId" : "refContact2",
  "body" : { 
    "LastName" : "Ross 2",
    "AccountId" : "@{refAccount1.id}"
    }
  },
  {
  "method" : "POST",
  "url" : "/services/data/v51.0/sobjects/Contact",
  "referenceId" : "refContact32",
  "body" : { 
    "LastName" : "Ross 3",
    "AccountId" : "@{refAccount2.id}"
    }
  }
  ]
}

Benefits of Salesforce Composite Connectors:

The Salesforce Composite resources within Salesforce’s REST API can be used to perform complex object interactions that would normally require multiple calls to Salesforce using the real-time API. In certain scenarios, this connector can simplify your flows, reduce the number of API calls to SFDC, and shorten processing time.   

The operations the Composite resources are:

  1. Batch: Batch allows up to 25 separate unrelated actions to be executed in a single call. Each executes independently, and information is not shared between the calls.
  2. SObject Tree: Creates one or more sObject_trees with root records of the specified type. An sObject tree is a collection of nested, parent-child records with a single root record.
  3. SObject Collections: Similar to batch, the SObject Collection resource can reduce round trip calls on groups of objects (up to 200) but requires a common action (e.g. Create, Update, Retrieve, or Delete a group of records). This call has the option to specify a rollback behavior in the case of partial failure.
  4. Composite: This will sequentially execute a series of actions in a single call. The response from one action can be used as an input in the subsequent action.


Code base @GitHub

https://github.com/VISHNUKVM/MuleSoft/blob/main/sfdc-composite-demo.jar

Sources:

https://developer.salesforce.com



No comments:

Post a Comment