DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Mule 3.9: Dataweave 1.0 Conditional Filtering

Mule 3.9: Dataweave 1.0 Conditional Filtering

In this article, I will explain how to leverage the Dataweave language on performing data filtering.

Enrico Rafols Dela Cruz user avatar by
Enrico Rafols Dela Cruz
·
Sep. 02, 18 · Tutorial
Like (2)
Save
Tweet
Share
40.80K Views

Join the DZone community and get the full member experience.

Join For Free

In 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:

Image title

Payload (computing) Contacts (Apple) Data Types Data (computing) Filter (software) Operator (extension) Record (computer science) Leverage (statistics) CSV

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Avoid “Schema Drift”
  • Comparing Kubernetes Gateway and Ingress APIs
  • 9 Ways You Can Improve Security Posture
  • How To Generate Code Coverage Report Using JaCoCo-Maven Plugin

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: