DEV Community

Cover image for Updating Record in Salesforce With Mulesoft
Crypto Geek
Crypto Geek

Posted on

Updating Record in Salesforce With Mulesoft

In this tutorial, we are going to build a simple mulesoft api that updates an account record in salesforce.

This article is written with an assumption that you have a basic knowledge of Mulesoft, Salesforce and also the required access to these applications.

We will follow the below steps to create a Mulesoft API that updates record in salesforce.

  • Create a Mule API
  • Configure salesforce connector
  • Create Mule Flow
  • Test the API

Create a Mule API

The first step is to create a new mule project in the Anypoint Studio tool. Follow the below steps to create a new mule project.

  1. Download and install the Anypoint studio on your PC.
  2. Open the the studio
  3. On the toolbar, click File -> New -> Mule Project.
  4. Enter the project name as sfdc-update

Configure Salesforce Connector

Lets add the salesforce connector to this mule project. Open the pom.xml file and add the following dependency.

<dependency>
  <groupId>com.mulesoft.connectors</groupId>
  <artifactId>mule-salesforce-connector</artifactId>
  <version>10.13.0</version>
  <classifier>mule-plugin</classifier>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Once you save the pom file, Anypoint studio will connect to the maven/mule exchange and downloads the required jar files.

Once the salesforce connector is added to your application, its time to create a connection to the salesforce.

Next, open the sfdc-update.xml file and go to the configuration XML tab. Add the following code between the xml tags.

<salesforce:sfdc-config name="Salesforce_Config" doc:name="Salesforce Config" doc:id="0e7bcc62-c0ba-4af1-8f3b-23fd83a2ee06" >
        <salesforce:basic-connection username="sfdcUser" password="sfdcPassword" securityToken="sfdcToken" url="sfdcURL" />
    </salesforce:sfdc-config>
Enter fullscreen mode Exit fullscreen mode

Here replace sfdcUser, sfdcPassword, sfdcToken and sfdcURL with your salesforce credentials.

Create Mule Flow

Create a mule flow using the following components.

  • Flow
  • Http Listener
  • Transform Message
  • Salesforce update component

The below code shows the complete mule flow for updating record in salesforce.

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
    xmlns:salesforce="http://www.mulesoft.org/schema/mule/salesforce"
    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/salesforce http://www.mulesoft.org/schema/mule/salesforce/current/mule-salesforce.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/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
    <salesforce:sfdc-config name="Salesforce_Config" doc:name="Salesforce Config" doc:id="0e7bcc62-c0ba-4af1-8f3b-23fd83a2ee06" >
        <salesforce:basic-connection username="sfdcUser" password="sfdcPassword" securityToken="sfdcToken" url="sfdcURL" />
    </salesforce:sfdc-config>
    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="59da75a4-21be-4dc2-88dc-33ef9b0f5bd4" >
        <http:listener-connection host="0.0.0.0" port="8081" />
    </http:listener-config>
    <flow name="sfdc-updateFlow" doc:id="c66b8839-0094-45d2-bf53-6842bf00a0c2" >
        <http:listener doc:name="Listener" doc:id="9f3abfa4-e288-4427-b7f7-0728886d8dec" config-ref="HTTP_Listener_config" path="/salesforce"/>
        <ee:transform doc:name="Transform Message" doc:id="359433a3-5998-47e6-9c6b-6d6355792300" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
[
    {
        "id" : payload.id,
        "name" : payload.name
    }
]]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <salesforce:update doc:name="Update" doc:id="109eb69f-46c4-4fcf-b6c7-81a07659e9b2" config-ref="Salesforce_Config" type="Account"/>
        <ee:transform doc:name="Transform Message" doc:id="64d5e74c-d78d-4d92-9d3f-70dcd1a74863" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
            </ee:message>
        </ee:transform>
    </flow>

</mule>
Enter fullscreen mode Exit fullscreen mode

You can read more about other salesforce DML operations here.

Test the API

It is time to test the API to whether it is working or not. Deploy the Mule API in the anypoint studio. The API by default runs on 8081 port.

Run the below curl command to test this api

curl -X POST \
  http://localhost:8081/salesforce \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: 24367e0a-2dc4-ea67-aa9d-bab35f246053' \
  -d '{
    "id" : "001000000000ABC",
    "name" : "Dev To"
}'
Enter fullscreen mode Exit fullscreen mode

Top comments (0)