Search code examples
muleanypoint-studiodataweave

Conditional expression is not working in Mule 4


I have this expression when setting the value of a variable in Mule:

#[(message.inboundProperties['message-id'] != null) ? message.inboundProperties['message-id'] : java.util.UUID.randomUUID().toString().replace('-', '')]

Basically, if the message does not already have an id allocated to it then it will have one created.

I have moved onto Mule 4 and Anypoint 7 and this expression no longer works. I know the inboundProperties has changed to attributes so have made the following changes:

#[(attributes.headers.'message-id' != null) ? attributes.headers.'message-id' : java.util.UUID.randomUUID().toString().replace('-', '')]

For both expressions I get the error "No viable alternative at input '('.

How can I fix this statement to work for Mule 4?

Thanks


Solution

  • #[attributes.headers.'message-id' default (uuid()  replace '-' with '')]
    
    1. expression use Dataweave 2.0 as default in mule 4, not MEL. So you can no longer use java method invocation. Instead use the uuid() dataweave function and the replace dataweave function

    2. You can use default instead of the if else check