Merging Records in Salesforce Using Merge connector
Let's take a look at why and how to use the salesforce merge connector provided by Mulesoft to Merge Records in Salesforce.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
The general work in Salesforce (SFDC) is doing the CRUD operation using Query, Delete, Create and Update (Upsert) operations.
I have found it difficult to find examples of how to merge requirements with Salesforce records. So in this article, I will explain, in detail, how to use the Salesforce Merge Connector.
Salesforce has provided 2 connectors for merging the salesforce records:
- Salesforce Merge Connector
- Salesforce Merge Multiple Connectors
The version of the connector, other minimal support requirements, and Connector configuration is mentioned at this Mulesoft Documentation link. The salesforce Merge connector can merge up to 3 records in 1 record at a time in Salesforce. If more than 3 records are required to merge into 1 record then Merge Multiple Connectors needs to be used.
Following is the complete XML code for implementing the following requirement.
- Fetch the Salesforce Contact Object Id. (Master Record)
- Fetch the Salesforce Contact Object Id. (Child Record)
- Perform the Megre operation and Merge the Child record in Master Record.
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:salesforce-composite="http://www.mulesoft.org/schema/mule/salesforce-composite" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:salesforce="http://www.mulesoft.org/schema/mule/salesforce"
xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns:anypoint-mq="http://www.mulesoft.org/schema/mule/anypoint-mq" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/anypoint-mq http://www.mulesoft.org/schema/mule/anypoint-mq/current/mule-anypoint-mq.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
http://www.mulesoft.org/schema/mule/salesforce http://www.mulesoft.org/schema/mule/salesforce/current/mule-salesforce.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/salesforce-composite http://www.mulesoft.org/schema/mule/salesforce-composite/current/mule-salesforce-composite.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="b67a5779-78ea-47e1-9e40-55c9aeb0fc8b" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<salesforce:sfdc-config name="Salesforce_Config" doc:name="Salesforce Config" doc:id="5f9a3188-bb45-4bdc-a6d5-5d2817d32766" >
<salesforce:basic-connection username="username" password="Password" securityToken="security Toek" url="https://login.salesforce.com/services/Soap/u/51.0" />
</salesforce:sfdc-config>
<flow name="salesforce-merge-flow" doc:id="730bf513-d11e-45a1-8949-84d5fc4cc50a" >
<http:listener doc:name="Listener" doc:id="758f2847-b011-42cd-a972-48e9f769fbe9" config-ref="HTTP_Listener_config" path="/getaccounts"/>
<salesforce:query doc:name="Query" doc:id="5564195f-d11f-44af-a329-368348dee445" config-ref="Salesforce_Config">
<salesforce:salesforce-query ><![CDATA[SELECT Id FROM Contact WHERE Name = 'Abhishek Solanki']]></salesforce:salesforce-query>
</salesforce:query>
<ee:transform doc:name="Transform Message" doc:id="df759e69-c1c8-4497-870b-e04537d5c77a" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
</ee:message>
<ee:variables >
</ee:variables>
</ee:transform>
<set-variable value="#[payload.Id]" doc:name="Set Variable" doc:id="ac67dc6f-be24-4fad-81fa-e166a390d78a" variableName="contactId1"/>
<salesforce:query doc:name="Query" doc:id="693c7fa4-28c9-4188-bedf-d2f643cd9fbc" config-ref="Salesforce_Config">
<salesforce:salesforce-query ><![CDATA[select Id from Contact where Name='Meetup Demo Contact']]></salesforce:salesforce-query>
</salesforce:query>
<ee:transform doc:name="Transform Message" doc:id="b30adf8a-0343-4169-9bd6-427e49a21324" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
</ee:message>
<ee:variables >
</ee:variables>
</ee:transform>
<set-variable value="#[payload.Id]" doc:name="Set Variable" doc:id="7cbac234-ce4f-4640-b702-b83f3d092aca" variableName="contactId2"/>
<ee:transform doc:name="Transform Message" doc:id="673aeb9b-fe23-4e15-aad8-5257de6cfbcd" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
flatten([vars.contactId2])]]></ee:set-payload>
</ee:message>
</ee:transform>
<salesforce:merge type="Contact" doc:name="Merge" doc:id="41ef54bb-1ede-4cda-b44a-9f12dac91ea3" config-ref="Salesforce_Config">
<salesforce:master-record ><![CDATA[#[{
Id: vars.contactId1[0]
}]]]></salesforce:master-record>
</salesforce:merge>
<ee:transform doc:name="Transform Message" doc:id="91477a6e-58e0-47bc-8846-cace5865682c" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
</mule>
The output will look like this:
{
"updatedRelatedIds": [
],
"mergedRecordIds": [
"0035g000009y2KkAAI"
],
"success": true,
"id": "0035g000009y1qgAAA",
"errors": [
]
}
Note: All the salesforce record id which is being passed in the connector payload areas should be an array of string.
- The master record id can NOT be a part of Ids to merge. So you need to separate the master id and child ids for merging.
- The master id in the configuration should be provided as an object:
{
Id: 'SFDC Object master record Id '
}
All complete details about the connector can be read at the link mentioned above.
Opinions expressed by DZone contributors are their own.
Comments