%dw 2.0 output application/json --- payload.event
Lab 1: Change Data Capture - gRPC
Overview
In this module we will set up Change Data Capture in Salesforce so we can start capturing events that occur within the Account object.
Change Data Capture (CDC) allows you to receive near real time changes of Salesforce records, and synchronize corresponding records in an external data store. CDC publishes change events, which represent changes to Salesforce records. These changes include creation of a new record, updates to an existing record, deletion of a record, and undeletion of a record.
Step 1: Setup CDC in Salesforce
In your Salesforce Org please navigate to the Setup. Once logged in you will find the Setup Icon on the right top.

Within the Setup, use the Quick Find and search for Change Data Capture.

Within the Change Data Capture Setup you will find the respective Objects/Entities in the left box.
Search for the Contact object and use the arrow button to move these to the Selected Entities in the right box and click Save.

Great! Lets move to the next step.
Step 2: Creating the Project
Anypoint Studio
Anypoint Studio is MuleSoft’s Eclipse-based integration development environment for designing and testing Mule applications.
Now let’s go build out our Mule Project! First we need to create a new Mule Project via File → New → Mule Project

Please enter your Projects name with your personal name such as <username>-salesforce-eapi and click Finish.

Next what we want to do is add our Salesforce Pub/Sub Connector into our Mule Palette. Click Search in Exchange and type in Salesforce Pub/Sub.

Next we want to add the module and also double check it is the latest version. Do this by clicking the add button in the middle of the screen followed by clicking on the drop down under version. Click Finish to move on to the next step.

Next click on our newly installed module, Salesforce Pub/Sub and drag and drop the Subscribe channel listener on to the canvas.

In the next step we will need to add our Salesforce Connector Configuration. Click once on the Subscribe channel listener element and you will see the configuration bar at the bottom of Anypoint Studio. Hit the green + button in order to create a new Configuration and enter your Salesforce Username and Password.

If you need a security token please refer to Step 2 in Mod 0

Test your connection by clicking Test Connection button and then click OK once successful and OK once more.
Now that the Connector Configuration is added, Anypoint Studio will crawl Salesforce for available Channels names. Please use the drop down box in order to select the Contact Change Event or type in /data/ContactChangeEvent. For replay options please select the Earliest from the drop down and leave the default batch event size for 100. This means that the Subscriber receives all events, including past events that are within the retention window of the server and new events sent after subscription.

Next let’s add a logger from the Mule Palette to our flow. Click Favourites in the Mule Palette and then drag and drop the Logger into the Process section of our flow.
Click the Logger once, followed by the little fx and then the 6 dots on the right hand side. We will be configuring what payload is being logged.

In the editor on the right hand side type in the following :
Changing the output to json will help the logged messages be more readable. We are also able to view the payload structure on the left hand side to correctly navigate to the changed data.

To complete the second lab we will now save our project. Click the floppy disk icon on the top left to save your project.

Well Done! Now on to the last step where we learn how to deploy and test.
Step 3: Deploy and Test
Running and Testing the Application Locally
Let’s locally kick start our application! To do this simply right click somewhere in the white space of your working canvas and select the first option Run Project.

The Anypoint Studio Console will now show up, once the Application is started successfully you should see the following in the console with status DEPLOYED.

Now that our app has successfully deployed let’s test out our flow. In order to see events in our Console, let’s move to Salesforce and make a change on an existing Contact.
In Salesforce navigate to the Contact object.

Within the Contacts, switch the view to All Contacts in order to see example Contacts that come with your Developer Org.

Open one random Contact and apply a Change to any field. For instance adjust the phone number and hit Save.
We recommend to have your Salesforce Org and Anypoint studio Side by side, so you can see the logging in real time with Anypoint Studio

Congratulations!!! You have successfully connected to Change Data Capture and are now able to view all the events that occur to Salesforce Objects.




