Mule 3.9: Dataweave 1.0 Conditional Filtering
In this article, I will explain how to leverage the Dataweave language on performing data filtering.
Join the DZone community and get the full member experience.
Join For FreeIn this article, I will explain how to leverage the Dataweave Language on performing data filtering. For instance, we need to prepare a payload for a Salesforce Upsert Operation where the initial records (Contacts) came from a CSV file and it should be enriched to include the lookup ID (Account) from Salesforce.
CONTACTS (CSV): Stored in our payload.
Account Name,Email,Home Phone,Mobile,Fax,Mailing Address,Status
Enrico R. Dela Cruz,rico@mail.com,14344,343434,34343,Maninla PH,Active
Lebron James,lbj@mail.com,5445,5434,5434,LA USA,Active
Kevin Durant,kd35@mail.com,2123,3221,1233,USA,Active
Kevin Love,love@mail.com,7689,7676,766467,USA,Inactive
ACCOUNT IDS (Salesforce): Assuming we already have the needed lookup ID as a Collection Data Type, which came from Salesforce Connector (Query). This value is stored in a variable lookupAccountIds.
[
{
Account_Name="Enrico R. Dela Cruz",
Account_Id="AC00001"
},
{
Account_Name="Lebron James",
Account_Id="AC00002"
},
{
Account_Name="Kevin Love",
Account_Id="AC00003"
}
]
Dataweave Script: This script filters the payload that contains the list of contacts before converting to an Object Data Type (application/java). Only contacts with an account name that already exists in Salesforce and has an active status are included on the output payload for Salesforce Upsert operation.
%dw 1.0
%output application/java
%var registeredAccountNames =
flowVars.lookupAccountIds.Account_Name
%function getAccount(item){
id: (flowVars.lookupAccountIds filter $.Account_Name == item."Account Name")[0].Account_Id,
activeFlag: true when item."Status" == "Active" otherwise false
}
---
payload filter ((registeredAccountNames contains $."Account Name") and $."Status" == "Active") map {
Account_Id: getAccount($).id,
Account_Name: $."Account Name",
Fax_Number: $."Fax",
Home_Phone_Number: $."Home Phone",
Mobile_Number: $."Mobile",
Mailing_Address: $."Mailing Address",
Active: getAccount($).activeFlag
}
I'm using a filter operator, which allows me to impart conditions of what records should be included on the map operation. Then, inside the map operation, to get the corresponding Account_Id from lookupAccountIds for a particular Contact, I created a function that filters the variable lookupAccountIds by the Account Name from the payload.
For more details about the Dataweave Operators, check this out: https://docs.mulesoft.com/mule-user-guide/v/3.9/dataweave-reference-documentation#operators
Testing:
Opinions expressed by DZone contributors are their own.
Comments