Skip to main content

#["123","456","789"0 personne en discute

Anna Törnblom a posé une question dans #[

The case:

I have an array of hotel IDs (hotelList), for each hotel I need to make an API call so I have a parallel for each where I handle each hotels call. If one of the calls fail I want to save/collect that hotel ID in a list (errorList) so that I can tell the source system which hotels failed. However I can't seem to populate the variable errorList when a call fails, instead it seems the temporary payload inside the for each carries the value but its then lost once another hotel is processed.

 

For simplicity I have a test case where I simply try to populate the errorList when i find "5" in the hotel ID, the hotelList is a hardcoded variable here. This is the xml:

 

<flow name="testFlow" doc:id="7b226885-c819-46a2-ae53-a249481eb8cc" >

<scheduler doc:name="Scheduler" doc:id="c51bef69-c084-415d-a731-f4cb2fd3d8e4" >

<scheduling-strategy >

<fixed-frequency frequency="9000000" startDelay="5" timeUnit="MINUTES"/>

</scheduling-strategy>

</scheduler>

<set-variable value='#["123","456","789"]' doc:name='hotelList: ["123","456","789"]' doc:id="748d293f-7f97-4d0d-9d6e-c7b19af8d125" variableName="hotelList"/>

<set-variable value="#[]" doc:name="errorList: []" doc:id="2ae34c4a-d74f-4e13-b306-7b9706a5a4ff" variableName="errorList"/>

<parallel-foreach doc:name="Parallel For Each" doc:id="3c81e54d-8e33-420d-9683-9877ceb94919" collection="#vars.hotelList">

<choice doc:name="Choice" doc:id="ac78978a-7581-4f7e-81df-1c0df6067289">

<when expression='#(payload contains ("5"))'>

<ee:transform doc:name="vars.errorList << payload" doc:id="8d30fabf-2a70-43ba-abae-28351f6d68e9">

<ee:message>

<ee:set-payload><![CDATA[%dw 2.0

output application/java

---

vars.errorList << payload]]></ee:set-payload>

</ee:message>

<ee:variables>

</ee:variables>

</ee:transform>

</when>

<otherwise>

<logger level="INFO" doc:name="Logger" doc:id="7e8f344d-845a-4902-8220-532bc8e8a757" message="Hotel is normal" />

</otherwise>

</choice>

</parallel-foreach>

<logger level="INFO" doc:name="vars.errorList" doc:id="2e18d4e7-629b-4b7f-9316-e0161e9da441" message="#vars.errorList" />

</flow>

 

How can I populate an array (variable) in a Parallel for each?

9 réponses
  1. 17 août 2022, 14:54

    Yes, this happens because in the transform message you are setting the payload instead of the variable errorList. See the difference below. You will also notice i've added a default [] in the transformation. When you do it like this, there is no need to declare the set variable before the for each loop. (Since it will initialize itself as an empty array the first loop).

     

    Your transformation:

    Yes, this happens because in the transform message you are setting the payload instead of the variable errorList. See the difference below.Transformation as it should be:

    image

0/9000