How can I migrate this expression code into mule 4

Viewed 17

I am migrating mule 3 code to mule 4 and this is the code in Expression component .How can I migrate this into mule 4.

{
if(vars.deletesVar != null)
{
vars.combinedArray.addAll(vars.deletesVar);
}
if(vars.insertsVar != null)
{
vars.combinedArray.addAll(vars.insertsVar);
}
if (vars.updatesVar != null)
{
vars.combinedArray.addAll(vars.updatesVar);
}
else{
vars.combinedArray=[];
}
}

I am new to Mulesoft, and working on migration. Can anyone help me what should I do in this to be work in mule 4 ?

1 Answers

You need to learn how to use DataWeave which is the expression language for Mule 4, instead of MEL which was the expression language for Mule 3. Read the migration guide to get tips on how to migrate your expressions.

Specifically in DataWeave you should return a single value and assign it to the target variable.

In this case you could replace the ifs using default so you can concatenate the arrays, if any of them is null it is replaced by the default, which I set to an empty array. It is simpler and the intention more clear in my opinion.

Example:

<set-variable value="#[(vars.deletesVar default []) ++ (vars.insertsVar default []) ++ (vars.updatesVar default [])]" doc:name="combinedArray" variableName="combinedArray"/>
Related