Jader Wallauer
Jader Wallauer

Reputation: 215

Is it possible to use MEL to do a dynamic variable name or something else?

I have a variable name="a#[ticket]" value="0"

The name will be a123456789 and the value will be 0

But how to access this value if i just print the a#[ticket] i will have a123456789 and what i need is #[a123456789] so it will give me 0.

The code so far:

<set-session-variable variableName="ticketId" value="a#[message.inboundProperties['ticket']]" doc:name="Variable"/>
<set-session-variable variableName="#[ticketId]" value="0" doc:name="Variable"/>

<logger message="#[#[ticketId]]" level="INFO" doc:name="messageID2"/>

and that last line will fail, but i dont know how to call that dynamic variable...

->mule version 3.4

Upvotes: 0

Views: 2488

Answers (2)

David Dossot
David Dossot

Reputation: 33413

You should be able to do the following:

<expression-component>t=message.inboundProperties['ticket'];flowVars['a'+t]='0'</expression-component>

to set a flow-var named from the concatenation of "a" and the value of the "ticket" inbound property.

To read it, use:

#[t=message.inboundProperties['ticket'];flowVars['a'+t]]

Note that if you copy the ticket inbound property in a flowVar with:

<set-variable variableName="ticket" value="#[message.inboundProperties['ticket']]" />

Then the syntax for assigning simply becomes:

<expression-component>flowVars['a'+ticket]='0'</expression-component>

and for reading:

#[flowVars['a'+ticket]]

Upvotes: 1

Jader Wallauer
Jader Wallauer

Reputation: 215

To set a dynamic variable name and use it with expression or logger you need to do as follow:

<set-session-variable variableName="a#[message.inboundProperties['ticket']]" value="0" doc:name="Variable"/>

That will set a dynamic name for a variable based on a ticket sent in the INBOUND message

Them i ve used a script to place it inside msgId:

    <scripting:component doc:name="Groovy">
        <scripting:script engine="Groovy">
            def String ticket = 'a' + message.getProperty('ticket', org.mule.api.transport.PropertyScope.INBOUND);
            return ["msgId":this[ticket]]
        </scripting:script>
    </scripting:component>

And that way i can access the variable value:

<logger message="#[msgId]" level="INFO" doc:name="variableValue"/>

Upvotes: 0

Related Questions