Mulesoft has so many connectors available that can connect to multiple applications. Mulesoft can also connect to the salesforce CRM application through the connector.
The salesforce connector is free to use and there is no need to purchase a separate license for it. in this article, we will see how to query data, and insert and update a record in salesforce with examples.
Before performing the operations, we have to add the salesforce connector as a dependency in pom.xml file. In Mule 4 version add the below dependency in the pom file.
<dependency> <groupId>com.mulesoft.connectors</groupId> <artifactId>mule-salesforce-connector</artifactId> <version>10.13.0</version> <classifier>mule-plugin</classifier> </dependency>
This will add the required salesforce jar files to our Mule project. Once the jar files are added, then we have to create a connection to the salesforce application.
Open any mule configuration file, switch to the configuration XML tag and add the below salesforce connection details.
<salesforce:sfdc-config name="Salesforce_Config" doc:name="Salesforce Config" doc:id="2bd3e4b6-676d-405f-8523-d531602a760b" > <salesforce:basic-connection username="sfdcUserName" password="sfdcPassword" securityToken="sfdcToken" url="sfdcURL" > <reconnection > <reconnect /> </reconnection> </salesforce:basic-connection> </salesforce:sfdc-config>
In the above connection, replace the below connection values with your salesforce app connection details.
sfdcUserName - user name sfdcPassword - password sfdcToken - token sfdcURL - test or prod url.
Now we are ready to perform the DML operations on the salesforce application.
In this example, we will try to fetch the recently created two account records from salesforce. The query to get the account data is
Select Id, Name From Account Order By CreatedDate desc limit 2
Drag the Query component of the salesforce connector into the Message Flow window. Select the connector configuration to the one that we have created in the above steps. Now copy and paste this salesforce SOQL query into the query window of the salesforce component.
Connect the Transform message component after this salesforce component. In the transform message component write the below DW code to get the salesforce data in a proper format.
payload map (sfdc, index) -> { Id: sfdc.Id, Name: sfdc.Name }
You can add more fields and also write complex SOQL queries like child-to-parent and parent-to-child relationships and fetch the data from the salesforce. I am leaving this to you as an exercise.
Let us insert a contact record in salesforce with an example. The create component of the salesforce connector accepts the data in java array format. We have to pass the appropriate data using the DW component. First, transform the data using the transform component as shown below.
%dw 2.0 output application/java --- [ { "FirstName": "BigData", "LastName": "Customers" } ]
Now drag and drop the create component after the above transform message. Configure this component with the connection created in the first step. Select the type field as Contact object and enter the payload in the records field. The XML code of this component is shown below:
<salesforce:create doc:name="Create" doc:id="97d4e3a2-1a61-4b47-ab4f-f6129d36079d" config-ref="Salesforce_Config" type="Contact"/>
This create component inserts the above record in the contact object. After the record is inserted, the create component returns the id of the record. The below DW code in the Transform component shows how to get the Id of the salesforce record.
%dw 2.0 output application/java --- payload.items map (sfdc, index) -> { Id: sfdc.id, Success: sfdc.success, Errors: sfdc.errors }
Here, the success field returns true or false. If the record is created, then it is true otherwise false. If the record creation fails, then the errors field contains the error message which we can use to analyze why the record creation failed.
Let us update the account record in salesforce with an example. Similar to the create component, the update component of the salesforce connector also accepts the data in java array format. When updating the record, we have to pass the id of the record and also the fields and data that we want to update. Also, we have to pass the data in java array format. First, transform the data using the transform component as shown below.
%dw 2.0 output application/java --- [ { "id" : "003000000D8acuIQAA", "Name": "BigDataers", } ]
Now drag and drop the update component after the above transform message. Configure this component with the connection created in the first step. Select the type field as Account object and enter the payload in the records field. The XML code of this component is shown below:
<salesforce:update doc:name="Create" doc:id="97d4e3a2-1a61-4b47-ab4f-f6129d36079d" config-ref="Salesforce_Config" type="Contact"/>
This update component updates the above record in the account object. After the record is updated, the update component returns the id of the record. The below DW code in the Transform component shows how to get the Id of the salesforce record and also the success message
%dw 2.0 output application/java --- payload.items map (sfdc, index) -> { Id: sfdc.id, Success: sfdc.success, Errors: sfdc.errors }
Here, the success field returns true or false. If the record is updated, then it is true otherwise false. If the record updation fails, then the errors field contains the error message which we can use to analyze why the record updation failed.
We can do many other operations like upserting, deleting, and bulk data processing, and even we can trigger an apex code as well. You can work on these operations as an exercise and learn the complete salesforce component.