Understanding Salesforce Composite Connector With MuleSoft
Salesforce Composite Connector provides various benefits like simplifying the code, reduce the network overhead by avoiding round trip between MuleSoft and Salesforce, Improve Application performance.
Join the DZone community and get the full member experience.
Join For FreeThis article will talk about the Salesforce Composite Connector and what are the various operations provided by the Salesforce Composite Connector. It will also talks about what are the different authentication mechanism supported to connect Salesforce and how to configure it.
Salesforce Composite Connector enable you to access various resources, objects or services in the Salesforce using the single API call. It is Anypoint Connector which allows you to use the Salesforce Composite Batch, Request and sObject tree api's.
It provides various benefits like simplifying the code, reduce the network overhead by avoiding round trip between MuleSoft and Salesforce, Improve Application performance.
This connector supports various Authentication mechanism are listed below.
- OAuth v2.0
- OAuth SAML
- OAuth JWT
- OAuth Username and Password
By default, Salesforce Composite Connector is not part of the Mule Palette, so you can installed from the Anypoint Exchange.
Here is the step by step video explaining how to configure the Authentication and installed the connector in the Mule palette.
Composite Request
Composite Request perform series of the rest api's call in single request. You can always use the output of the one request as the input to the subsequent request. Requests in the composite call is known as subrequests. You can have up to 25 subrequest in single call.
It provides a single response which includes all subrequest responses. Top level request always returns HTTP Status Code as "200".
In below request body, there is the 2 subrequests. The first creates the Account in the salesforce and assigned output to the refAccount. The second creates the contact under the new parent Account by referencing the refAccount in the body.
Request Body
xxxxxxxxxx
{
"compositeRequest" : [{
"method" : "POST",
"url" : "/services/data/v51.0/sobjects/Account",
"referenceId" : "refAccount",
"body" : { "Name" : "Jack John" }
},{
"method" : "POST",
"url" : "/services/data/v51.0/sobjects/Contact",
"referenceId" : "refContact",
"body" : {
"LastName" : "John",
"AccountId" : "@{refAccount.id}"
}
}]
}
Response Body
xxxxxxxxxx
{
"compositeRequest" : [{
"method" : "POST",
"url" : "/services/data/v51.0/sobjects/Account",
"referenceId" : "refAccount",
"body" : { "Name" : "Jack John" }
},{
"method" : "POST",
"url" : "/services/data/v51.0/sobjects/Contact",
"referenceId" : "refContact",
"body" : {
"LastName" : "John",
"AccountId" : "@{refAccount.id}"
}
}]
}
Here is the video tutorial explaining how to configure the Composite Request.
It also support the Transaction by using attribute allOrNone. By default allorNone is false, in case if you want to enable the transaction, you can add allOrNone attribute to true in he request body. This will ensure either all subrequest process successfully or nothing will be committed in case of single or multiple failures.
xxxxxxxxxx
{
"allOrNone": true
"compositeRequest" : [{
"method" : "POST",
"url" : "/services/data/v51.0/sobjects/Account",
"referenceId" : "refAccount",
"body" : { "Name" : "Jack John" }
},{
"method" : "POST",
"url" : "/services/data/v51.0/sobjects/Contact",
"referenceId" : "refContact",
"body" : {
"LastName" : "John",
"AccountId" : "@{refAccount.id}"
}
}]
}
Here is video tutorial explaining how to configure the Composite Request with Transaction.
Composite Batch
Composite Batch allows you to perform up to 25 independent subrequest in the single call. All the subrequests count against the rate limit. The response bodies and HTTP statuses of the subrequests in the batch are returned in a single response body.
Batching for the following resources and resource groups is available in API version 34.0 and later.
- Limits—vXX.X/limits
- sObject resources—vXX.X/sobjects/
- Query—vXX.X/query/?q=soql
- QueryAll—vXX.X/queryAll/?q=soql
- Search—vXX.X/search/?q=sosl
- Connect resources—vXX.X/connect/
- Chatter resources—vXX.X/chatter/
In below request body, there are the 3 subrequests. The first is updating the name in the Account objects. The second is fetching the particular account Name and Billing Postal Code. The third request is sending query to retrieve Id and Name for particular account.
Request Body
xxxxxxxxxx
{
"batchRequests": [
{
"method": "PATCH",
"url": "v51.0/sobjects/account/0013B00000iP47yQAC",
"richInput": {
"Name": "John Jim"
}
},
{
"method": "GET",
"url": "v51.0/sobjects/account/0013B00000iP47yQAC?fields=Name,BillingPostalCode"
},
{
"method": "GET",
"url": "v51.0/query?q=Select Id,Name from Account Where Id='0013B00000iP47yQAC'"
}
]
}
Response Body
xxxxxxxxxx
{
"hasErrors": false,
"results": [
{
"statusCode": 204,
"result": null
},
{
"statusCode": 200,
"result": {
"attributes": {
"type": "Account",
"url": "/services/data/v51.0/sobjects/Account/0013B00000iP47yQAC"
},
"Name": "John Jim",
"BillingPostalCode": null,
"Id": "0013B00000iP47yQAC"
}
},
{
"statusCode": 200,
"result": {
"totalSize": 1,
"done": true,
"records": [
{
"attributes": {
"type": "Account",
"url": "/services/data/v51.0/sobjects/Account/0013B00000iP47yQAC"
},
"Id": "0013B00000iP47yQAC",
"Name": "John Jim"
}
]
}
}
]
}
Here is the video tutorial explaining how to configure the Composite Batch.
sObject Tree
sObject tree creates one or more sObject trees with root record of the specified type. It is the collection of parent-child, netsed with a single record type. There are few limitations with sObject tree as it allows only 200 records across all the trees, allows up to 5 records of different type and and it is up to 5 level deep.
In below request body, there are 2 account creation request with 2 contact for each account.
Request Body
xxxxxxxxxx
{
"records" :[{
"attributes" : {"type" : "Account", "referenceId" : "ref1"},
"name" : "Jit John",
"phone" : "1234567890",
"Contacts" : {
"records" : [{
"attributes" : {"type" : "Contact", "referenceId" : "ref2"},
"lastname" : "Smith",
"Test1__c": "test123",
"email" : "sample@salesforce.com"
},{
"attributes" : {"type" : "Contact", "referenceId" : "ref3"},
"lastname" : "Evans",
"Test1__c": "test123",
"email" : "sample@salesforce.com"
}]
}
},
{
"attributes" : {"type" : "Account", "referenceId" : "ref4"},
"name" : "Jim John",
"phone" : "1234567890",
"Contacts" : {
"records" : [{
"attributes" : {"type" : "Contact", "referenceId" : "ref5"},
"lastname" : "Jack",
"Test1__c": "test123",
"email" : "sample@salesforce.com"
},{
"attributes" : {"type" : "Contact", "referenceId" : "ref6"},
"lastname" : "Jin",
"Test1__c": "test123",
"email" : "sample@salesforce.com"
}]
}
}]
}
Response Body
xxxxxxxxxx
{
"hasErrors": false,
"results": [
{
"referenceId": "ref1",
"id": "0013B00000iP49fQAC"
},
{
"referenceId": "ref4",
"id": "0013B00000iP49gQAC"
},
{
"referenceId": "ref2",
"id": "0033B00000boceSQAQ"
},
{
"referenceId": "ref3",
"id": "0033B00000boceTQAQ"
},
{
"referenceId": "ref5",
"id": "0033B00000boceUQAQ"
},
{
"referenceId": "ref6",
"id": "0033B00000boceVQAQ"
}
]
}
Here is the video tutorial explaining how to configure the sObject Tree.
References
https://docs.mulesoft.com/salesforce-composite-connector/2.8/
Conclusion
Salesforce Composite Connector is Anypoint Connector which reducing the multiple calls towards Salesforce, avoiding round tripping between MuleSoft and Salesforce, simplifying the code as you don't have to orchestrate the multiple calls towards Salesforce and improve the performance of your applications.
Opinions expressed by DZone contributors are their own.
Comments