MuleSoft offers more than 100 connectors that can be used to create APIs and batch processes. These processes can read information from multiple sources, transform that information, and use it to update different target systems. In this tutorial, we are going to show how to create a Mule batch process that listens in Salesforce for new won opportunities and creates a new card in Trello with the name of those opportunities.
If you want to try the code of this article, you can clone this repository: https://github.com/jrhuerga/sfdc_oppty2trellohttps://github.com/jrhuerga/sfdc_oppty2trello
Step 1: Create Flow
The first step is to use Anypoint Studio to create a new Mule Project. In this case, I am going to name it “sfdc_opptytrello”:
Step 2: Detect New Opportunities
The first component that we are going to use is a “Scheduler”. The Scheduler component enables you to trigger a flow when a time-based condition is met. In this case, we are going to trigger the flow every 30 seconds:
Now, we are going to use the Salesforce Connector of MuleSoft. The Anypoint Connector for Salesforce lets you connect to the Salesforce platform. The connector exposes convenient methods for exploiting the capabilities of Salesforce, including working with Apex classes. In this case, we are going to use the Query component, which executes a query against the specified object and returns data that matches the specified criteria. The returned fields are String and, if necessary, you can convert them to the appropriate type using Weave and Transform Message for example. We are going to execute this SOQL query to find Opportunities in state ClosedWon modified today:
SELECT Id, Name, account.name, LastModifiedDate from Opportunity
where IsClosed = true and IsWon = true and LastModifiedDate = TODAY
As it may be possible that the result set from Salesforce could contain zero opportunities, we check if the output of the payload is an empty array so the rest of the flow is only executed when the size of the array is greater than zero:
Step 3: Create Trello Cards
In case Salesforce is not returning an empty array, we take from Salesforce the name of the Opportunity and the name of the Account. In this example, we are just using the first opportunity in the array. In case there are more opportunities, it may be interesting to improve this flow to iterate through all the won opportunities.
With the information that Salesforce has for the opportunity, we prepare a payload that will be sent in a POST to the Trello API. In this example we are using a fixed List in a Trello Board that we refer to by using its ID:
Once the payload needed by the API of Trello is ready, we use the Trello connector of MuleSoft to create a new card:
When Trello creates the new card, it returns a JSON message confirming the operation. We use part of that response to write in the log the URL assigned to that new card:
Step 4: Use Object Store to Prevent Duplicates
So far, we are creating a new card for every opportunity that is in stage ClosedWon and has been modified today. However, if we do not improve the flow, we are going to get a new duplicated card in Trello created by the flow every 30 seconds. In order to avoid that, we can use the Object Store of MuleSoft.
An object store is a facility for storing objects in or across MuleSoft applications. MuleSoft uses object stores to persist data for eventual retrieval. Internally, MuleSoft uses object stores in various filters, routers, and other message processors that need to store the state between messages.
To use the Object Store, first, we check if the ID of an opportunity is already in the Object Store. We store the result of that check-in, as a variable named “found”:
Then, we create a Choice message processor to check if the variable “found” contains a “true” value (the ID is already in Object Store, so we do not continue the flow) or “false” (the ID is not already in Object Store, so we have to create the card in Trello):
We add a message processor to the flow to store the ID of the opportunity before creating the card in Trello, so the next time the flow is executed, the card will not be created again:
This screenshot shows the complete flow with all the message processors:
Example of Execution
To check if the flow is working as expected, we login to Salesforce to create a new opportunity named “Sales of merchandising” for the account “GenePoint”. We set the opportunity Stage as “Value Proposition”:
If we check the log of the flow, we will see that every 30 seconds it is displaying a message saying that there are not opportunities modified today in state Closed Won:
The next step in Salesforce is to change the Stage of the opportunity to Closed Won:
After 30 seconds, we will see in the log a new message, saying that a new card named “Sales of merchandising” has been created, providing a link to the newly created card:
If we check in Trello, we will see that now there is a new card named “Sales of merchandising”:
If we click on the card, we will see that in the description field it is included the name of the Account (GenePoint):
Now, if we check again in the log, we will see that every 30 seconds, the flow detects that there is an opportunity, but as its ID is already in Object Store, it does not try to create a new card for that opportunity:
We have seen in this tutorial how easy it is to use MuleSoft to create a batch process that periodically checks if there are new won opportunities; the flow uses the Trello connector of MuleSoft to create a new card with the name of the opportunity and the account. MuleSoft provides hundreds of connectors, so it is possible to extend this flow to trigger additional actions, for example sending an email, writing a message in Slack, sending the data to SAP, etc.
If you want to try the code of this article, you can clone this Github repository: https://github.com/jrhuerga/sfdc_oppty2trello