How to integrate Salesforce Platform Event using MuleSoft Anypoint Studio?

on

|

views

and

comments

Dear #Muleys,

In this blog post, we are going to discuss how to use Platform Event to integrate Salesforce using MuleSoft.

Prerequisite

  1. Salesforce Account
  2. MuleSoft AnyPoint Studio Installed into Local PC

Step1 – Setup Platform Event

The very first step is to set up the Platform Event inside our Salesforce Org. To do that navigate to Setup -> Integrations -> Platform Event and then Click on New

Check the below Image for the complete Platform Event

Step2 – Create MuleSoft Project

Create a MuleSoft project and drag & drop Subscribe Channel Listener component related to Salesforce

Configure the Connection for salesforce and then

open the properties and provide the Platform event that you have created under Streaming Channel

Here is the Structure that a Platform Event will send to MuleSoft From Salesfore

Below is the Response body which will be contained by payload.data.payload

{
  "Name__c": "Test Account",
  "Annual_Revenue__c": 9034.0,
  "CreatedById": "0050o00000XaBL9AAN",
  "Description__c": "Test Description",
  "CreatedDate": "2021-03-19T11:28:21.049Z",
  "Phone__c": "9087654321",
  "Id__c": "0010o00002jEAV4AAO",
  "Active__c": false
}

Now drag & drop the Logger component and log the payload to see the result.

Step3 – Run MuleSoft Project

Now, Right Click on the Canvas and then click on Run Project “Project Name” wait till the Project is up and running.

Step4 – Publish the Platform Event using Apex

Now, as we have done the Platform Event setup and MuleSoft Project it’s time to publish the Event. Use the below code to publish the Event.

PAccount__e event = new PAccount__e (
    Name__c ='Test Account',
    Id__c ='0010o00002jEAV4AAO',
    Annual_Revenue__c  = 9034,
    Description__c  = 'Test Description',
    Phone__c = '9087654321'
);
Eventbus.publish(event);

Go back to your MuleSoft Console and you will see the output like this

{
  data={
  schema=Pi-cIAgMf-j1hFCDNLJ7-g, 
  payload={
    Name__c=Test Account, 
    Annual_Revenue__c=9034.0, 
    CreatedById=0050o00000XaBL9AAN, 
    Description__c=Test Description, 
    CreatedDate=2021-03-19T11:46:39.136Z, 
    Phone__c=9087654321, 
    Id__c=0010o00002jEAV4AAO, 
    Active__c=false
  }, 
  event={
    replayId=3766742
  }
}, '
channel=/event/PAccount__e
  
}

Here is the XML file

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

<mule xmlns:validation="http://www.mulesoft.org/schema/mule/validation" 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
http://www.mulesoft.org/schema/mule/validation http://www.mulesoft.org/schema/mule/validation/current/mule-validation.xsd">
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="6517b989-a1b6-439e-b2c8-fd4e33ed48ee" >
		<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="d8676059-3155-4436-afd5-dbdfdc6c8f41" >
		<salesforce:basic-connection username="yourusername@salesforce.com" password="YOUR_PASSWPRD_HERE" />
	</salesforce:sfdc-config>
	<validation:config name="Validation_Config" doc:name="Validation Config" doc:id="1a067602-0645-4157-b594-8170df3e1284" />
	<flow name="jwt-sfdcFlow" doc:id="e0d47b85-092b-498d-8a70-cb4eb73a0361" >
		<http:listener doc:name="Listener" doc:id="416fc317-0dd8-4575-8df9-05eb87e05ddb" config-ref="HTTP_Listener_config" path="/mule"/>
		<salesforce:query doc:name="Query" doc:id="a80b8c69-edd3-4959-95f6-2f65d347379a" config-ref="Salesforce_Config">
			<salesforce:salesforce-query ><![CDATA[SELECT Id, Name, Email, Phone FROM CONTACT LIMIT 10]]></salesforce:salesforce-query>
		</salesforce:query>
		<ee:transform doc:name="Transform Message" doc:id="7d305128-7145-4e4c-be3d-0310bbd66aab" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
			</ee:message>
		</ee:transform>
	</flow>
	<flow name="jwt-sfdcFlow1" doc:id="be01a0d9-1b57-4e13-840e-258934095a5a" >
		<salesforce:subscribe-channel-listener streamingChannel="/event/PAccount__e" doc:name="Subscribe channel listener" doc:id="a86de055-b443-4811-b3ac-c2e2247d9021" config-ref="Salesforce_Config"/>
		<logger level="INFO" doc:name="Logger" doc:id="40556a19-2ad4-451f-9ce4-11c66095aae6" message="#[payload]" />
		<ee:transform doc:name="Transform Message" doc:id="a650e036-c84e-4087-8e5e-24184a2b90d9" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload.data.payload]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<validation:is-not-null doc:name="Is not null" doc:id="ed0a5b20-24b6-4d05-934e-a933f251bb24" value="#[payload.Id__c]" message="Record Id can not be null" config-ref="Validation_Config"/>
		<logger level="INFO" doc:name="Logger" doc:id="65735cce-2d6a-49f4-9c27-8d249688b098" message="#[payload]"/>
	</flow>
</mule>

Happy Learning 🙂

Thanks for reading if you have any questions, please feel free to connect to me

#Mule4 #Muleys #MuleSoft

Amit Singh
Amit Singhhttps://www.pantherschools.com/
Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion
Share this

Leave a review

Excellent

SUBSCRIBE-US

Book a 1:1 Call

Must-read

How to Utilize Salesforce CLI sf (v2)

The Salesforce CLI is not just a tool; it’s the cornerstone of development on the Salesforce Platform. It’s your go-to for building, testing, deploying, and more. As one of the most important development tools in our ecosystem

Save the day of a Developer with Apex Log Analyzer

Table of Contents What is Apex Log Analyzer? Apex Log Analyzer, a tool designed with Salesforce developers in mind, is here to simplify and accelerate your...

Salesforce PodCast

Introduction Hey Everyone, Welcome to my podcast, the first-ever podcast in India for Salesforce professionals. Achievement We are happy to announce that we have been selected as Top...

Recent articles

More like this

8 COMMENTS

  1. Hi Amit, How would salesforce listens back the PE Channel. I have a requirement where we are doing integration with platform events, so we publish it to middleware system but we also wants to listen to the response being returned and process it in salesforce. So in this case would I be needing 2 PE channels one for sending payload and another for receiving response ? As I assume if SF subscribes to same channel being used by middleware it will be a looping situation.

    Thanks.
    Princy

  2. Hi Amit,

    Thanks for such informational blog. I always learn something new through your blogs or video. thank u so much..

    I have created a list of events and published the eventlist. that code ran successfully in salesforce. but in mulesoft i am getting only one event record in payload.

    could you please help me here.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

5/5

Stuck in coding limbo?

Our courses unlock your tech potential