Logo

dev-resources.site

for different kinds of informations.

Updating Record in Salesforce With Mulesoft

Published at
1/9/2023
Categories
mulesofthackathon
salesforce
Author
cryptogeekus
Categories
2 categories in total
mulesofthackathon
open
salesforce
open
Author
12 person written this
cryptogeekus
open
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
mulesofthackathon Article's
30 articles in total
Favicon
Top Use Cases of MuleSoft API Manager
Favicon
Introducing Sowoe Creative Agency: Crafting Bold, Impactful Stories for Your Brand
Favicon
Download the song Kar from the past work
Favicon
How the Kellogg Innovation Network is Transforming Collaboration and Innovation
Favicon
How a Tech Company in Pakistan is Driving Innovation
Favicon
Unlocking the Power of Integration with MuleSoft
Favicon
Transform Your Body at Home: Effective Weight Loss Exercises Without a Gym
Favicon
Hello Mule (Mulesoft)
Favicon
Best practices for Mulesoft SAP integration
Favicon
Learn MuleSoft Training to become a Master in Anypoint Studio
Favicon
Mulesoft Training Interview Questions and Answers 2023
Favicon
Updating Record in Salesforce With Mulesoft
Favicon
What does MuleSoft Composer let you do?
Favicon
Why businesses should consider resorting to MuleSoft?
Favicon
Working of Mulesoft?
Favicon
Where to Learn Mulesoft?
Favicon
Mulesoft external API for custom alerts creation, automatic application restart and Jira ticket creation - Mule version 4
Favicon
Everyday is an API day with Jotto 2020
Favicon
Missing kids alerting system
Favicon
Slack Bot for Support Personnel build with API Led Connectivity
Favicon
Movid - An Alexa skill that provides Covid-19 symptom checking and real time information.
Favicon
Share your Dinosaurs
Favicon
Everyday is an API day with BlackJack
Favicon
TravelZoom- Mule Integration With Everyday API's
Favicon
IoT + MuleSoft - Sense room temperature and send message to mobile
Favicon
Cloud Hub Alexa Skill Set -Command your IAAS to work for you
Favicon
Control Your Mule Infra Via Bot
Favicon
Good Sleep: Controlling the temperature in a remote room through IOT devices and MuleSoft!
Favicon
Talk to your Toaster with Mule
Favicon
Subscription Model for News and Stocks via MuleSoft and Kafka

Featured ones: