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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Rust’s Ownership and Borrowing Enforce Memory Safety
  • Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • DataWeave Interview Question: Compare IDs From Two Arrays and Create a New Array

Trending

  • Designing Memory Architecture for Stateful AI Agents: Short-Term Context and Long-Term Persistence
  • Evaluating RAG in Production — What the Numbers Actually Revealed
  • Three Measured Outcomes of Manually Scaling Fleet in Prod
  • Every Acquisition Promises One Company. Most Deliver Two.

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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.

By 
Enrico Rafols Dela Cruz
·
Sep. 02, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
43.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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,[email protected],14344,343434,34343,Maninla PH,Active
Lebron James,[email protected],5445,5434,5434,LA USA,Active
Kevin Durant,[email protected],2123,3221,1233,USA,Active
Kevin Love,[email protected],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.

Related

  • Rust’s Ownership and Borrowing Enforce Memory Safety
  • Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • DataWeave Interview Question: Compare IDs From Two Arrays and Create a New Array

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook