Trigger Events in Salesforce; receive them in Mulesoft.

We all know that Mulesoft is now part of Salesforce. It's been more than two years, since Salesforce acquired it.

But even before the acquistion, those two have had a strong integration. 

In my opinion, as a person who has been working in the integration space for more than 20 years,  one of the most atractive things within their integration though the Mulesoft Salesforce connector, it is the ability to suscribe to events that ocurr within Salesforce.

One of the challenges that has always been present when you are integrating an application, it is the  ability to generate events, that 3rd parties can subscribe to. This is a good element for an Event-Driven-Architecture (EDA), and really a differentiator if you are building a connectivity/integration initiative.

Well, Salesforce offers a lot of alternatives to generate events. Not only within Salesforce boundries, but also accross them. And one key player here for that regard, is Mulesoft via its Salesforce Connector.

Salesforce offers:
  • Push Topic Event
  • Generic Event
  • CDC
  • Platform Event
  • etc
Which are different alternatives to generate events within Salesforce, after a change has occurred in one of its objects or when you programmed that the the event should occur (APEX,Workflow).

Some of them are more flexible than others in terms of the customization and data that the event can contain.

But something that they have in common, is that they can be used by the Mulesoft Salesforce Connector, and with that, trigger a mulesoft flow. And now within a mulesoft Flow, you can pretty much do whatever you want with the data and send it to other systems, or publish the information through another event mechanism for 3rd parties.

Let's imagine you want to inform another system, every time a create, update or delete for a lead happens But you do not want to be polling Salesforce every now and then to look-up for new leads, what you want is to be notified that the event happened and then manipulate the information and send it to others or process them, or whatever you want to do with it.

In the last parragraph I mentioned "you do not want to be polling", I mentioned it, because that is a common practice, but a very bad one. Rather than that, you will subscribe for events, and only when they happend and you are ready to process them , you will. That is cleaner and more efective approach, and it can help you to understand the value of an event-driven-architecture.

Mulesoft is a very versatile platform, it offers an API Platform, but also an API implementation and integration platform. But not only that,  it can be used to expose asynchronous APIs, to serve as part of an EDA architecture.

In this post I am going to show you how we can create a Mulesoft Flow that can react to three different events generated by Salesforce:

1. On Create Event
2. On Modify Event
3. On Delete Event

We will use Lead as the entity in Salesfroce, to which we are interested to know if a new lead was created; or if a lead was updated or if it was deleted.

But again, we want to react to the event. We are not going to be asking to Salesforce if a new Lead was created, updated or deleted. No! We are going to subscribe and react. 

In order to do that, let's create a Mulesoft project that will look like this:


We have three diffrent flows, one per event. After we receive the event, we simply send it to the logger.

In order to achieve this, we need in general two things:

  1. Create the Mulesoft project and use the Salesforce connector. In order to configure the Salesforce connector, you need a developer account (https://developer.salesforce.com/)
  2. Create a Lead in Salesforce and play with it: update it and delete it.

The steps, are:

  • Create a Mulesoft Project: 

  • Name it: triggers01, or any name you would like to use.
  • Once with the project created, let's create a properties file that will include the Salesforce connectivity information

          The token is generated via the developer.salesforce.com portal, as well as the user and pwd
  • The properties (yaml) file needs to be located in the folder that is indicated in the previous image
  • Now let's create two entries in our Global Elements tab:

  • For the Salesforce Configuraton, you should input this:

      As you can see we have configured username, password and token, getting the values from the               properties file . It is a good practice to use this type of configuration. Once tu typed it , click on             Test Connecton:


  • Now is time to create the flow. As we've mentioned in previous lines, we will react to events, therefore we are not going to drag and drop http listeners. We are going to drag and drop a Salesforce Connector capability.
  • Let's start with the On Create Event. But before that, be sure you have added into your Mule Palette, the Salesforce Connector. Like this:


  • Now, let's add the On New Object. Please locate it, and drag and drop it in the Canvas:

  • Now you should have this:

  • Repeate the same formula for these two events:


  • Drag and drop them into the canvas. And now you should have this:

  • Now let's add a logger activity into the On New Object flow, and configure it like this:

  • Repeate the same formula for the other two flows. Now you will have this:




  • Now let's configure the three events. Let's start with the On New Object event. Double click the activty and configure it like this:

    As you can see we've configured different elements:

  1. Display Name: It can be whatever you want
  2. Connector Configuration: It should be already selected, since you created in previous steps
  3. Object Type: This is the entity to which you want to be inform if a new one is create, modified or deleted. In this case we are going to use Lead
  4. Scheduling Strategy: It is the frequency to which we are going to get the event messages. 
  5. Since: This is a key element , because events in Salesforce are independant to the Connector, so if you are already publishing events , when you start the Mulesoft app, it will retrieve the messages that were created before. But if your choose a date, then only the events created after that date, will be retrieved. So be careful with this field
  6. Frequency: It is related with the Scheduling Strategy, and in our case , will be every five seconds
  7. Time Unit: In this case, seconds
  • Now let's do the same thing for the On Modify and On Delete, like this:

and


  • Now we are ready to test this . Right click your project and Run the project. The project should be in the DEPLOYED status. Like this:


Now are are ready to create some Leads in Salesforce, and see how our Mulesoft application reacts to them.

Go to https://salesforce.com and login with your credentials, and look for the Leads tab:


(Here you see it in spanish, because I am from Mexico and my defaults are in Spanish. But in your case -English- it is Lead).

Let's create a new Lead:


Just fill the necessary elements, and click create the lead. After that, get back to Anypoint Studio, and see what was written in the log:




You have two new lines, and if you scroll to the right, you will see the details of the Lead you've created:


Why two?

I think every time you create an object, it generates two events: on for creation, and one for modify.


Now let's test, the modify event. Let's edit the lead changing the contact email:


Let's edit the email address:


After you've edited , you will see a 3rd line in your log, where you can see the updated email address:



Pretty simple, right?

With this, you can change the perspective on how you can integrate Salesforce, with other applications. The connector is very powerful, and the events layer in Salesforce as well. And they are integrated, so you can do the math and conclude that this is a great ability within Mulesoft.




Comentarios

Entradas populares