Integration · MuleSoft · Salesforce

Return Requested Data from Salesforce Multiple Data Sources using MuleSoft

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.

Response Criteria:

  • 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

Required Softwares:

  1. Anypoint Studio (version: 7.3.2)
  2. Postman

Mule Flow:

  1. In Anypoint Studio, Go to file -> select new Mule project -> Name the project.
  2. Now from Mule palette drag and drop the Listener followed by Set Variable component. So this is our main flow.
  3. 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.

Capture

flow1.PNG

flow3

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.

Fetch_From_org1 Process:

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.)

Fetch_From_org2 Process:

Similarly query and transform the results of second org to required JSON format and set the result payload to another variable named ‘org2_payload’.

Main Flow:

Now the main flow starts with the listener followed by Set Variable component which stores the Account Code send in the request.

Postman Request:

Postman 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.

Code:

code1code2code3code4code5

Output for 3 conditions:

This slideshow requires JavaScript.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s