ReynierPM
ReynierPM

Reputation: 18660

How to build a multiple IF conditional in Mule 4 and DW 2.0?

I need to create a function with conditions like this pseudo-code:

var consent = []

function buildConsent() {
   if (condition1) {
       consent += values1
   }

   if (condition2) {
       consent += values2
   }

   if (condition3) {
       consent += values3
   }
}

This is how I am doing it on Mule4 and DW 2.0:

%dw 2.0
var consent = []
var factIntake = vars.facts

fun buildConsent() =
    if (factIntake.miscFactItems[?($.value1 == true)] != null) {
        consent + {
            "Consent_Type": "some1",
            "Consent_Given_By": "some2"
        }
    }

    if (factIntake.miscFactItems[?($.value2 == true)] != null) {
        consent + {
            "Consent_Type": "some3",
            "Consent_Given_By": "some4"
        }
    }

output application/json
--
{
    "Consent_Data": buildConsent()
}

But I am getting the following error from the IDE (AnypointStudio 7):

Invalid input '+', expected Namespace or Attribute<'@('(Name:Value)+')'> (line 11, column 11):

Where line 11, column 11 is the first appearance of consent +. If I try to debug the project all I got in the console is:

Message : Error while parsing script: %dw 2.0

Here is an example of input/output for you to better understand what I am trying to achieve:

// Input
{
    "miscFactItems": [{
            "factId": "designeeFirstName",
            "factValue": "test test",
            "factValueType": "System.String"
        }, {
            "factId": "designeeLastName",
            "factValue": "test test",
            "factValueType": "System.String"
        },{
            "factId": "value1",
            "factValue": true,
            "factValueType": "System.Boolean"
        }, {
            "factId": "value2",
            "factValue": true,
            "factValueType": "System.Boolean"
        }, {
            "factId": "value3",
            "factValue": true,
            "factValueType": "System.Boolean"
        }
    ]
}

// Output
consent = [{
             "Consent_Type": "type1",
             "Consent_Given_By": miscFactItems.designeeFirstName
         }, {
             "Consent_Type": "type2",
             "Consent_Given_By": miscFactItems.designeeFirstName
         }, {
             "Consent_Type": "type3",
             "Consent_Given_By": miscFactItems.designeeFirstName
         }
]

What I am missing here? How do I add the three conditions to my function and append the values to the consent var?

Upvotes: 1

Views: 12512

Answers (2)

Shoki
Shoki

Reputation: 1538

In DataWeave variables are immutable, so you can't accumulate things in the same variable, you need to create new variables.

So it would look something like this:

%dw 2.0
output application/json  

var consent1 = if (condition1) [{"Consent_Type": "some1", "Consent_Given_By": "some2"}] else []
var consent2 = if (condition2) [{"Consent_Type": "some3", "Consent_Given_By": "some4"}] else []
---
consent1 ++ consent2

Upvotes: 3

Imtiyaz Qureshi
Imtiyaz Qureshi

Reputation: 281

Your requirement looks like a good use of reduce function. Based on the pseudo code you provided, you can do something like below

output application/json
var payload = [  
    {"name":"Ram", "email":"[email protected]", "state": "CA","age":21},  
    {"name":"Bob", "email":"[email protected]","state": "CA","age":30},
    {"name":"john", "email":"[email protected]","state": "NY","age":43} 

] 
---
payload reduce ((item, consent = []) -> consent +
{
    (state: item.state) if(item.state=='CA'),
    (age: item.age) if(item.age >25)
}
)

Upvotes: 0

Related Questions