DataWeave with Flow Reference Lookup

(0 reviews)

home

This application uses pre-packaged tools to append data to a message payload and perceptively connect with Salesforce. The example takes a CSV file of new account information, figures out which region each account belongs to, appends region information to the payload, then uploads the new accounts to an active Salesforce user account. It uses Mule DataSense and Anypoint DataWeave to map and transform data, thereby facilitating quick integration with this Software-as-a-Service (SaaS) provider.

2db66700-createsalesforceaccountflow.png

Connect with Salesforce

At times, you may find that you need to connect one or more of your organization's on-premise systems with an SaaS application, such as Salesforce. Ideally, these independent systems would talk to each other and share data to enable automation of end-to-end business processes. Use Mule applications to facilitate communication between your on-prem system(s) and Salesforce. (Though this use case does not extend as far, you can also use Mule to facilitate communication between SaaS providers.)

DataWeave and FlowRefLookup

Beyond transforming and mapping data from one format to another, you can use an Anypoint DataWeave Transformer to access other flows in a Mule application to acquire additional information. Use a flow lookup to acquire information outside the message, then append it to the payload.

Assumptions

This document assumes that you are familiar with Mule and the Anypointβ„’ Studio interface.

This example also assumes that you have a basic understanding of Mule flows, Mule Global Elements, and Studio's Anypoint DataWeave.

This document describes the details of the example within the context of Anypoint Studio, Mule ESB’s graphical user interface, and includes configuration details for XML Editor where relevant.

Example Use Case

The use case upon which this example is based represents a reasonably common requirement to upload new account information into Salesforce. From a CSV file containing information about new accounts (company name, billing address, etc.), a user wishes to use the addresses of the companies to determine the sales region to which each company belongs, then upload all the account details – including sales region – to Salesforce. This example Mule application performs these actions.

Set Up and Run the Example

Complete the following procedures to create a custom field and the example application. Then, run this example in your own instance of Anypoint Studio. Skip ahead to the next section if you prefer simply to examine this example.

To see the end-to-end functionality, you must have an active Salesforce account into which you don't mind inserting a new custom field for "region" and two sample accounts. After this application automatically uploads these example customer accounts, you can manually delete them and the custom field from your Salesforce account.

Create a Custom Field in Salesforce
  1. Log in to your Salesforce account, then navigate to the Setup tab. Now, click on Customize > Accounts > Fields under the Build section on the left side of the page.
  2. Click New under the Account Custom Fields & Relationships section.

    • In Step 1 of the new field process, select Text, then click Next to continue.
    • In Step 2, enter values for your new field as per the table below, then click Next to continue. Field Label RegionLength 50Field Name Region
  3. In Step 4, check the Account Layout box, then click Save.

  4. Your new field name appears followed by a double-underscore and a lowercase "c": Region__c This is the new field to which DataWeave will map the region data it acquires from another flow.
Create the Example Application
  1. Open the Example project in Anypoint Studio from Anypoint Exchange. Do not run the application.
  2. Log in to your Salesforce account. From your account menu (your account is labeled with your name), select Setup.
  3. In the left navigation bar, under the Personal Setup heading, click to expand the My Personal Information folder.
  4. Click Reset My Security Token. Salesforce resets the token and emails you the new one.
  5. Access the email that Salesforce sent and copy the new token into your local clipboard.
  6. In your SaaS Integration application in Mule Studio, click the Global Elements tab.
  7. Double-click the Salesforce global element to open its Global Element Properties panel. In the Security Token field, paste the new Salesforce token you copied from the email. Alternatively, configure the global element in the XML Editor.
  8. Change the contents of the Username and Password fields to your account-specific values, then click OK to save your changes.
  9. This project includes a sample CSV file, called companies.csv, that you can use to see the end-to-end functionality of the application. In the Package Explorer, click the src/main/resources folder to expand it, then find the contacts.csv file inside this folder.
  10. Double-click the File Config option in the Global Elements tab and set the Working Directory field to point to src/main/resources folder.
Run the Example Application
  1. In the Package Explorer, right-click the dataweave-with-flow-reference project name, then select Run As > Mule Application. Studio runs the application on the embedded server. The File Endpoint in the application polls the input CSV file every ten seconds and processes it.
  2. In your browser, access your Salesforce account, then navigate to the Accounts tab.
  3. Use the drop-down menu to display All Accounts, then scan your contacts for two new entries: - Universal Exports
    • Best Widgets
  4. You should get the following output in your console window: State to lookup is: FLINFO 2014-07-22 12:36:45,800 [REFORMAT0_0] org.mule.api.processor.LoggerMessageProcessor: Region is : South EastState to lookup is: CAINFO 2014-07-22 12:36:45,804 [REFORMAT0_0] org.mule.api.processor.LoggerMessageProcessor: Region is : West Coast
  5. Stop the Mule application by clicking the square, red button in the Console.
  6. Delete the two sample accounts from your Salesforce account.
  7. Delete the custom field Region from your Salesforce account.

How it Works

Using two flows, this application accepts CSV files which contain account information, uses the "state" data to append a sales region to the message, then uploads the contacts to Salesforce.

CreateNewSalesforceAccountFlow

The Scheduler component triggers the CreateNewSalesforceAccountFlow every ten seconds. The File Component reads the input CSV file and passes its content to the Anypoint DataWeave Transformer. The Transformer converts the format of the data from CSV to a collection and maps the input fields from the CSV file – company_name, company_address, etc. – to output fields that Salesforce uses in a collection.

The DataWeave also utilizes a flow lookup. The llow lookup accesses another flow in the application to acquire the sales region for each new account. DataWeave invokes LookupSalesRegionFlow, which uses the company_state data to determine into which sales region the account falls. DataWeave then maps this newly acquired data to the custom field in Salesforce, Region\_\_c.

When it has converted all the account information in the file to a collection of Salesforce-friendly data, the application uses a Salesforce Connector to push data into your Salesforce account. The connector's configurations specify the operation – Create – and the sObject type – Account – which dictate exactly how the data uploads to Salesforce; in this case, it creates new accounts.

LookupSalesRegionFlow

This flow consists of a Groovy component and a Logger. The script in the component uses state information in the message payload to calculate the sales region to which the account belongs. Invoked by the flow lookup in DataWeave, this flow exists only to determine a sales region for each account in the CSV file.


Reviews

TypeExample
OrganizationMuleSoft
Published by
MuleSoft Organization
Published onSep 6, 2018
Asset overview

Asset versions for 2.1.x

Asset versions
VersionActions
2.1.4
2.1.3
2.1.2