user3483129
user3483129

Reputation: 137

Create an arraylist in mule and iterate over it

I want to create an arrayList and iterate over its entries to check if my message has the set properties

        <set-variable variableName="ex" value="#[{'A', 'User-Agent', 'Application-ID', 'API-Key', 'P', 'Organization-ID'}]" doc:name="Set Variable" />
        <for-each collection="#[ex]" doc:name="Foreach">
           <choice doc:name="Choice">
             <when expression="#[message.inboundProperties.contains([payload]) == false]">
                <set-variable variableName="isRequestValid" value="false" doc:name="Variable"/>
             </when>
           </choice>
        </for-each>

I am getting an error at the for-each expression (Invalid content was found starting with element 'for-each'). Further can we use choice block without otherwise option?

Upvotes: 0

Views: 7017

Answers (4)

Manik Magar
Manik Magar

Reputation: 1401

  1. It should be foreach
  2. List can be initialised as #[['A','B']]
  3. If you don't need otherwise i.e. just filter out payload, you can use expression-filter inside foreach -
    <set-variable variableName="ex" value="#[['A', 'User-Agent', 'Application-ID', 'API-Key', 'P', 'Organization-ID']]" doc:name="Set Variable" />
    <foreach collection="#[ex]" doc:name="Foreach">
        <expression-filter expression="#[message.inboundProperties.contains([payload]) == false]" doc:name="Expression"/>
        <set-variable variableName="isRequestValid" value="false" doc:name="Variable"/>
    </foreach>

Upvotes: 1

AnupamBhusari
AnupamBhusari

Reputation: 2415

As you have already resolved the issue with for-each, remaining issue can be resolved using following expression

#[message.inboundProperties[payload] !=null]

If it returns true then header present otherwise not.

Hope this helps.

Upvotes: 0

sulthony h
sulthony h

Reputation: 1277

It shall be foreach instead of for-each.

We can use Choice block without otherwise option. But it will introduce runtime error when the value does not match with the logic in when option.

Upvotes: 0

Ale Sequeira
Ale Sequeira

Reputation: 2039

You are probably using an old Mule version, for-each construct was included in 3.4 (see release notes), I cannot think of an other reason for that error. Unfortunately you cannot use choice without otherwise, but there is an improvement reported: https://www.mulesoft.org/jira/browse/MULE-6129

Upvotes: 0

Related Questions