Blog

Get Our Latest Thoughts & Opinions

Salesforce Platform Events with Mulesoft API

  • 11 Jan 2022
  • Yurii Kostenko

In this article, we are going to build Salesforce platform events and learn how to use Mulesoft to subscribe to those events. All you need to have to do this is a Salesforce developer account and Mulesoft Anypoint studio

Platform events enable different systems to work with Salesforce data through loose coupling between them - multiple systems can subscribe to the same Salesforce channel and receive its data, without knowing about the existence of each other. You can implement ‘Broadcast Pattern of the event-driven architecture’ by using Salesforce events and Mulesoft APIs (more on this in our next post). 

We choose the ‘Account’ object for our platform events in Salesforce and are going to use the following fields: Id, Name, Type, Phone, Active__c. In this example, we use Process Builder to publish new platform events.

Steps:

1. Let’s start by creating a platform event in Salesforce:

a) Go to Setup -> Integrations -> Platform Eventsb) On the Platform Events page, click “New Platform Event

c) Define Label, Plural Label, and Description. For “Publish Behavior”, keep the default of “Publish After Commit”. Click “Save

d) Add custom fields in the ‘Custom Fields and Relationships’ related list (make sure that the data types of custom fields matches the fields to be chosen from Account object later on (alternatively, you can always choose the “Text” as data type)


2. Create a new process in Process Builder to publish events:

a) Go to Setup -> Process Automation -> Process Builder 

b) Create a new Process: define a Process Name (API Name will be auto-populated), and Description. For “The process starts when”, choose “A record changes”. Click “Save"

c) Choose ‘Account’ as Object initiating the Process. For “Start the process”, choose “only when a record is created”. Click “Save"

d) Add criteria: define Criteria Name. For “Criteria for Executing Actions”, choose “No criteria - just execute the actions!”. Click “Save

e) Click on ‘Add Action’ and select the action type “Create a Record”. Define “Action Name”, for “Record Type”, choose the value of a label you gave to a Platform Event in the step 1.c

f) Set field values: find a platform event field, change “Type” to “Field Reference”, and for “Value” find an Account object field you want to get the value from. Click “Save

g) Click on “Activate” to start the process

3. Let’s create a Mule Project in Anypoint Studio and add logic to it.

a) Open Anypoint Studio and Create a new Mule Project, define ‘Project Name’ and click “Finish

b) Add Salesforce Module to Mule Palette of your project

c) Drag & drop “Subscribe channel listener” operation to a working canvas

d) Add connector configuration to connect to your Salesforce developer org (you need to get the API token first if you choose Basic Auth connection).

e) For “Streaming Channel” in General settings, choose your event name

f) You can see the structure of the input payload received from Salesforce event, and can transform it with ‘Transform Message’ component next in your flow (we added a simple transformation for value coming from “Active” event field transforming it from text to boolean type, and assigning false value if this field is empty).

g) Add the Logger component at the end of the flow with the payload in the message to log the results of the transformation

h) Save the project

4. Run Mule Project

a) Run the project locally in Mulesoft Anypoint Studio: right-click on the canvas and choose “Run project ”. Wait a few seconds until the project is deployed


5. In the last step, we will test our solution by creating a Salesforce event, and see the data in the logs of Mule Project

a) Go to your Salesforce developer org, and create a new Account (leave “Active” picklist unpicked

b) See the logs in Anypoint Studio for received events

With these 5 easy steps, we have created an event-driven solution by using Salesforce platform events and Mulesoft API.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Today's post is from one of our Mulesoft Architects, Yurii Kostenko.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

As always, thanks for reading, if you enjoyed this post please feel free to share it and tag us @Pexlify.

Pexlify is a leading digital transformation partner with Salesforce.com.

Want to know more about our Salesforce SolutionsContact us today to set up a hassle-free consultation.

Get In Touch

Discover how Pexlify can create digital experiences that transforms and optimises your business with Salesforce.

Get Started