Hear from Salesforce leaders on how to create and deploy Agentforce agents.
Contact Us 1-800-596-4880

Reference Multiple Inputs

This DataWeave example takes three different input JSON files, one in the payload, another in a variable and the third in an attribute. All of them are parts of the same Mule Event. The payload contains an array of book objects, the variable has a set of currency exchange rates, and the attribute specifies a year to be used as a query. Before you begin, note that 2.x versions of DataWeave are used by Mule 4 apps. For DataWeave in Mule 3 apps, refer to DataWeave version 1.2 examples. For other DataWeave versions, you can use the version selector in the DataWeave table of contents.

The example uses the following:

  • filter to leave out the books that are older than the date specified in the incoming attribute.

  • map to go through each book in the incoming array. Another map function then goes through the currencies in the variable to calculates the book’s price in each one. A third map function lists out all authors of a book in case there are more than one.

  • @ to define an XML attribute in the output.

DataWeave Script:
%dw 2.0
output application/xml
---
books: {
  (payload filter ($.properties.year > attributes.publishedAfter) map  (item)   ->  {
    book @(year: item.properties.year): {
      (vars.exchangeRate.USD map {
        price @(currency: $.currency): $.ratio * item.price
      }),
      title: item.properties.title,
      authors: { (item.properties.author map {
        author: $
      }) }
    }
  } )
}
dataweave
Input JSON Payload:
Input Attributes:
{
  "publishedAfter": 2004
}
json
Input Variable exchangeRate:
{
  "USD": [
    {"currency": "EUR", "ratio":0.92},
    {"currency": "ARS", "ratio":8.76},
    {"currency": "GBP", "ratio":0.66}
  ]
}
json
Output XML: