In the continuously evolving digital world, organisations are accumulating exponentially larger volumes of customer data, and it often becomes fragmented, duplicated, inconsistent and out-of-date as it travels across different systems and users. With Salesforce CRM being the single source of truth for many organisations, it is important to ensure the accuracy and reliability of customer data, not only within Salesforce but within every system that holds customer information.
In this article we will look in detail at how a customer-related event that is created or updated in Salesforce CRM, is captured, processed, and synchronised with other business systems of the organisation. We will cover the following in detail:
- What is Salesforce CRM?
- What is a Streaming API and why is it useful?
- What is a PushTopic?
- Use case: Capturing the details of the Account that is created/updated in Salesforce and processing the event using Mule 4
What is Salesforce CRM?
Salesforce is a leading enterprise SaaS platform and CRM (Customer Relationship Management) and is one of the core capabilities that Salesforce offers. It can manage all the customer interactions of an organization through different media, like phone calls, site email enquiries, communities, as well as social media. Salesforce handles all customer relationships, by focusing on sales, marketing and support processes.
What is a Streaming API and why is it useful?
Tracking data changes in Salesforce is especially useful when you have business data stored in a system external to Salesforce. You can use a Streaming API to keep your external source in sync with your Salesforce data with change data capture events and PushTopic events. Also, Streaming APIs let you process business logic in an external system in response to data changes in Salesforce. In this article we will cover PushTopics.
What is a PushTopic?
A PushTopic is an sObject (Salesforce Object) that contains the criteria of events you want to listen to, such as data changes for a particular object. You define the criteria as an SOQL query in the PushTopic and specify the record operations to notify on (create, update, delete, and undelete).
In this section, we will look in detail at creating a PushTopic to capture changes on an Account (Salesforce Object) and processing the events using Mule.
1. Steps to create a PushTopic in Salesforce
These instructions assume that you have a Salesforce Org created with access to the Account object.
- Login into Salesforce Org
- Click on the Salesforce Setup icon and select ‘Developer Console’ -> ‘Debug’-> ‘Open Execute Anonymous Window’ -> Copy the below script to the window and execute.
PushTopic PushTopic = new PushTopic();
PushTopic.Name = ‘AllAccounts’;
PushTopic.Query = ‘SELECT Id,FirstName,LastName,Name,Salutation,Phone From Account’;
PushTopic.ApiVersion = 51.0;
PushTopic.NotifyForOperationCreate = true;
PushTopic.NotifyForOperationUpdate = true;
PushTopic.NotifyForOperationUndelete = false;
PushTopic.NotifyForOperationDelete = false;
PushTopic.NotifyForFields = ‘All’;
We have successfully created a PushTopic on Account object.
2. Subscribing to the Push Topic
Subscribe to the PushTopic that we just created to see the activity when Accounts are created or updated.
Below are the steps to subscribe to a PushTopic to view the events:
- Go to Workbench.
- Select either Production or Sandbox depending on which environment your Salesforce organization is in.
- Click Login with Salesforce.
Make sure that you log in with the same Salesforce account for the Salesforce organization that you are setting up with the integration.
- Go to queries > Streaming Push Topics.
- From the Push Topic menu, select the name of the PushTopic that you created.
- Click Subscribe.
- A message stating “Connected” shows above the PushTopic. Once you create or update an Account in Salesforce, you will see activity as below. In the below example the event is captured for a PersonAccount created.
3. Processing the event message using MuleSoft
Now that we have our PushTopic capturing updates on Account, let’s look at processing the event using MuleSoft
Below are the steps required to create a listener in Mule 4:
- Create an empty Mule 4 application and add Salesforce connector dependency in pom.xml
- Add the “Salesforce Replay Topic Listener” component and configure it to listen to the topic we just created.
- In the example below, the payload from the Salesforce PushTopic is transformed and used to upsert the customer information in a DB using a Stored Procedure, thus keeping Customer information in Salesforce and DB in sync.
- Below is the xml snippet of Salesforce configuration and replay topic listener component
<salesforce:sfdc-config name=”Salesforce_Config” doc:name=”Salesforce Config” doc:id=”a7fb8b33-ed70-4f85-b8f6-ad7af7f395f4″ >
<salesforce:basic-connection username=”username” password=”password” securityToken=”token” />
<salesforce:replay-topic-listener topic=”AllAccounts” replayOption=”ONLY_NEW” doc:name=”replay-sfdc-customer-topic-listener” doc:id=”0995d776-1a15-42c1-8b98-451a01060db0″ config-ref=”Salesforce_Config”/>
The Mule application is now ready to be started/deployed.
- When the Mule application starts it listens to the “All Accounts” PushTopic. On an create/update on Account event it logs the payload.
- The Mule application can also be deployed to CloudHub
- For simplicity, the Mule application is deployed in Anypoint Studio and then we are going to test the application by creating Customers (Person Accounts) in the Salesforce Organization.
- Below is the message that is logged when a Customer (Person Account) is created in our Salesforce Organization.
- The customer information is synced to the DB using a stored procedure.
In this article, we have covered creating a PushTopic on Accounts object in Salesforce and processing the event messages using Mule 4.
Other useful links: