This blog walks you through the Integration between Salesforce and Mulesoft Anypoint Studio. Consider a scenario that returns requested records from multiple Salesforce orgs. Consider Account object with an External ID field named as Account Code which allows alphanumeric inputs. This flow will receive the Account Code as a JSON input via a HTTP listener and it will search for the Account Code from two Salesforce Orgs and return the Account and its related opportunity information as a response to the HTTP request.
- If there is no record in Org 1 with the matching Account Code, then there should be an Error JSON returned to the user.
- If there is no record in Org 2 with a matching Account Code, then the response should return with blank values in the JSON fields.
- If there is record in both the Org’s with a matching Account Code, then the response should return combined records from both Org’s as a single JSON
- Anypoint Studio (version: 7.3.2)
- In Anypoint Studio, Go to file -> select new Mule project -> Name the project.
- Now from Mule palette drag and drop the Listener followed by Set Variable component. So this is our main flow.
- In order to create seperate flows Fetch_From_org1 and Fetch_From_org2, drag and drop the Query Salesforce connector to the canvas away from the mainflow. This will automatically create a seperate flow.
So there are 2 flows named Fetch_From_org1 and Fetch_From_org2 other than the main flow. Fetch_From_org1 handles salesforce org1 and Fetch_From_org2 handles salesforce org2.
Use the Salesforce Query connector and fetch the Account and its related opportunity records with the matching Account code sent from the Postman http request. Add Transform Message Component to transform the query results to desired JSON format. Finally set the result payload to a variable named ‘org1_payload’. (In mule we can use the variable content through out the flow.)
Similarly query and transform the results of second org to required JSON format and set the result payload to another variable named ‘org2_payload’.
Now the main flow starts with the listener followed by Set Variable component which stores the Account Code send in the request.
Using Flow Reference component we can switch to another flow that exists in the same project. So refer these two flows in the main flow with the help of Flow Reference. Add the choice component to check the conditions(Choice component is similar to if and else conditional statement). Here we have three conditions to check.
- First check if the payload of org1, stored in org1_payload variable is empty, then set another variable with error message. Now set the payload with that error message variable and use Transform Message component to transform the error message to JSON format.
- Check if the payload of org2, stored in org2_payload variable is empty if so then set another variable to null. Now set the payload with that null variable and use Transform Message component to transform the error message to JSON format.
- This is default block where records exists in both the orgs. Use Transform Message component to combine the payload from both orgs to JSON format.
Output for 3 conditions: