How can I stop <foreach> loop in between in mulesoft

6.6k views Asked by At

I have an array list values which I loop using foreach and choice. As soon as the first success happens in choice I want to come out of the foreach loop. And I don't want continue to check remaining conditions. In mulesoft flow

Thank you in advance.

6

There are 6 answers

1
Sagar Chaudhari On BEST ANSWER

You might not be able "break" foreach loop, but you can always achieve this functionality using expression/groovy script. Please refer this: Is there a break statement in Mule <foreach>

1
vijay dhanakodi On

As you have mentioned in your use case Wrap the message filter inside your for-loop.

<foreach doc:name="For Each" collection="#[]">
    <message-filter onUnaccepted="flow" >
        <expression-filter expression="#[]"/>
    </message-filter>
</foreach>
1
RakeshSharma227 On

First of all you should find out on which index you will get success message by using indexOf method. Then after use subList method to get the required list and use that list in Foreach loop.

 <foreach collection="#[payload.subList(0,3)]" doc:name="For Each">
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
    </foreach>
1
Raju Mali On

Please use Break Statement for exit from foreach loop according to your condition true.

0
Sanjeet Pandey On

There is no component to break the foreach loop. If you only want to process till the success condition, then you could use MEL to execute the foreach on a sublist like this:

<foreach collection="#[payload<condition>]" />
 ...
</foreach>

You can also use loops in Groovy.

Example. In a payload i have some HashMap -

def nap = payload.Result
 for (v in nap)
 {
     if (v.Name == '!!! а папка')
     {
         assert v.Id != null
         flowVars['folderid'] = v.Id
         payload = 'recived Id of folder object is '+v.Id
         break
     }

 }
 return payload
0
Möoz On

Use Groovy

You can control the flow of a Groovy script much easier than a traditional "for-each" transformer. In your particular case, your groovy script would look something (but not exactly) like:

<scripting:component doc:name="Groovy">
 <scripting:script engine="Groovy"><![CDATA[// Define the collection to loop
    def loopCollection = flowVars.nameOfCollectiontoLoop

    // Define the condition which needs to be met
    def exitCondition = "Some condition to match"

    // Loop the collection to do what you need
    loopCollection.each { row ->
        if (row.identifierName == exitCondition)
        {
            return
        }
        else
        {
            // Continue your processing
        }   
    }

    return payload]]></scripting:script>
</scripting:component>

Use a Choice strategy to stop processing

The other way which I can think of is to use a Choice as soon as you enter the loop to see if the condition has been previously met, if so, do nothing; as follows:

<flow name="testsFlow">
        <set-variable variableName="conditionMet" value="#[false]" doc:name="conditionMet"/>
        <foreach doc:name="For Each">
            <choice doc:name="Choice">
                <when expression="#[(flowVars.conditionMet == true)]">
                    <scripting:component doc:name="Do nothing">
                        <scripting:script engine="Groovy"/>
                    </scripting:component>
                </when>
                <otherwise>
                    <scripting:component doc:name="Continue Loop">
                        <scripting:script engine="Groovy"><![CDATA[// Define the collection to loop
def loopCollection = flowVars.nameOfCollectiontoLoop

// Define the condition which needs to be met
def exitCondition = "Some condition to match"

// Define the "conditionMet" variable
def conditionMet = flowVars.conditionMet

// Loop the collection to do what you need
loopCollection.each { row ->
    if (row.identifierName == exitCondition)
    {
        conditionMet = true
    }
    else
    {
        conditionMet = false
        // Continue your processing
    }   
}

return payload]]></scripting:script>
                    </scripting:component>
                </otherwise>
            </choice>
        </foreach>
    </flow>

Try these and let us know if you need more help.