How does loop in workflow work?

I've created my first workflow and I think I'm doing something wrong with my loop.
What it should do:

  • Fetch list with trackingno's from Retool DB
  • loop through this list and for each trackingno get the status from UPS
  • for each trackingno, check the statuscode, if status = 011, kickoff api call, else kickoff other api call

For now it seems that it evaluates the checkStatus block only once and not for each trackingno
How to get this working?

Hey mbruijnpff,

Loop should return an array of results - for each value passed.

Do you mind expanding the JSON output to view the structure of the data and see if it passed only one value?
Assuming GetParcelTracking is an API call it might be worth adding await before the trigger so the requests do not time out due to some API limits.
image

From there we can work on pinpointing what is happening!

The loop is outputting an array with objects:
image

One object for each trackingno, so that seems to be ok.

How go go further and finish the workflow for each object? For delivered items something needs to be done and not delivered once will be polled another time.

In that case you need a branch to check if loop returns results and if it fits some of your additional criteria (i.e. loopTrackno.data.length>0). Then all you need to do is loop through "loopTrackno" results and do the desired action.

For the ones that need to be polled another time you can use the getTrackno and exclude the items present in loopTrackno and set the status or whatever you want it to do.

I've tried adding a branch after the loop, see my first post. If statuscode is 011 (delivered) it will doe something. else it should update the Retool DB with last polling time and current status, so it can be checked again later.

Did I use the branch wrong?

Now it seems that it is only evaluating the first object in the returned array

If I'm not mistaken Branch just represents a visual IF statement and it won't loop through the results. In that case you are just checking the first shipment and status code of it.

I've attached an image with an example on how this could work:

Let me know if this helps!

1 Like

I have a similar workflow, but I do not know how identify which record succeeded for failed.

My flow:

  1. Query database for emails not yet sent. Output: result set.
  2. Transform results ready for Mailgun. Output: The JSON I need to send to Mailgun including record ID.
  3. Loop through all results and send emails. Output: The id of the sent email.
  4. ?? If I put another loop here, how do I tie the database record ID to the email id I just got back? I need to update the correct DB record with the results of sending the email. I could assume that the order of the array of results form step 2 are the same as the array from step 2, but that seems risky and I =have no way of verifying that.

I was able to find a workaround, but I would still very much like to know the right way to do this.

My workaround was to Loop through all of the transformed results and call another workflow that actually sent the emails. Since the called workflow is only sending one email at a time, it knows which one succeeded or failed and can update the correct database record.

1 Like

I like that solution, @bradlymathews ! Apologies for the delay on this one. I think you may also be able to solve this by customizing the loop code to to return data from the value as well as the loop result: