1
votes

My company is moving toward migrating our current Mule 3.6 APIs into Mule 4.2 and I'm trying to migrate our first API at present. There are numerous differences between the runtimes not least the wide use of Dataweave 2.0 in Mule 4. I'm new to a lot of the components of Mule 4 having used Mule 3 extensively, and I'm currently stuck with moving the following expression component into Dataweave. I'm struggling to get the correct syntax without Studio complaining that there are errors.

The current expression is

<expression-component doc:name="Expression"><![CDATA[
flowVars.pSector="ELECTRICITY";
if(flowVars.serialNo.length()==14){
if (flowVars.serialNo.substring(0,2)=="G4" || flowVars.serialNo.substring(0,2)=="E6" || 
flowVars.serialNo.substring(0,2)=="JE" || flowVars.serialNo.substring(0,2)=="JA" || 
flowVars.serialNo.substring(0,2)=="JS") {
flowVars.pSector="GAS";     
}
}]]></expression-component>

this is essentially determining the fuel type of a meter based on component parts of its serial number and it's length. Any help on converting this expression into a Dataweave would be appreciated

1

1 Answers

1
votes

Note that in Mule 4 there can not be side effects, meaning that you can assign the result of one script to the payload or to one variable. Also DataWeave is functional rather than an imperative language. In Mule 4 variables are referenced as vars.name instead of flowVars.name.

A naive translation could be like this:

        <ee:transform doc:name="Transform Message">
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/java
fun checkSerial(serial)=if (sizeOf(serial) == 14 )
    if (serial[0 to 1] == "G4" or serial[0 to 1]=="E6" or serial[0 to 1]=="JE"
        or serial[0 to 1]=="JA" or serial[0 to 1]=="JS") 
        "GAS"
    else 
        "ELECTRICITY"
else "ELECTRICITY"
---
checkSerial(vars.serialNo)]]></ee:set-payload>
            </ee:message>
        </ee:transform>