Help Needed: Troubleshooting Workflow Loops for Lead Distribution

Hi Glide Community!

First off, I just want to say how much I appreciate this community. You all provide so much value, and I’m hoping someone can help me troubleshoot a problem I’m having with loops.

What I’m Trying to Do:

I’m building a lead distribution workflow that:

  1. Reads an Excel file attached via email, converts it to JSON, and loops through each lead to assign it to an agent.

  2. Agents have a predefined distribution percentage.

  3. Leads are assigned randomly, but the assignment respects the agents’ distribution percentages using cumulative math (e.g., Random Number < Cumulative Percentage).

  4. Each lead should only be assigned to one agent, and the loop should stop for that lead once it’s assigned.

The Current Workflow:

Loop All Leads: Loops through all the leads extracted from the email file.

Within Loop All Leads:

• Sets cumulative values for agents based on their distribution percentage.

• Loops through the agents to find one where Random < Cumulative.

• Assigns the lead to the agent if the condition is met.

The Problem:

• The agent loop (Loop Through Agents) keeps iterating even after the lead is assigned to an agent.

• It results in multiple leads being assigned incorrectly or more iterations than expected (e.g., 28 iterations for 4 leads and 7 agents).

• I tried using conditions like “if the Email field is not empty, skip,” but I can’t seem to apply this logic effectively within the workflow.

What I’ve Checked:

• Cumulative math appears to work as expected (logs show it hitting 100).

• The issue seems tied to how the workflow handles exiting or skipping when a lead is already assigned.

Screenshots:

  1. Workflow Setup:

  1. Logs:

• Shows how cumulative math is calculated correctly.

• Also shows the excess iterations in the agent loop.

I’d really appreciate guidance on:

• How to exit a loop or skip iterations after a condition is met (e.g., lead is already assigned).

• If there’s a better way to structure this workflow logic to avoid unnecessary iterations or start in a new direction for a round-robin type of lead distribution based on pre-defined %?

Thank you so much in advance for your time and expertise! If you need more details, I’m happy to provide them. :pray:

I think this is your main problem. Loops are designed to loop through every row that matches the condition and there’s no current way to tell it to stop looping when a condition is matched, or when it has its first “completed” loop.

Is there a way for you to update a calculation for all rows after every iteration, so that the iterations after your successful assignment can return a false scenario?

I think the issue is that, because I’m pulling from a JSON array, I can’t actually write back to it to create a flag or mark rows as “processed” after an assignment. So, what I’m leaning toward now is creating a staging table instead.

Here’s my plan:

  • Write the JSON objects into a staging table. That way, I can loop through them and update individual rows (like adding an Assigned = false/true flag).
  • Then, as each lead is assigned, I’ll write it to my main Leads table.
  • Once the loop is done, I’ll add a cleanup step to clear the staging table so it doesn’t grow unnecessarily.

At first, I thought about skipping the staging table and writing directly to the Leads table, but then I realized it would eventually have to filter through every single lead, which could mean going through thousands of rows someday. I figured that’d be way less efficient and might eat up a lot more iterations.

Thanks for helping me reason through this. I’ll report back if I’m successful!

That might be a way to approach it, my question was more on the table where you do the assignments. Is there a feasible way to update the calculations after each successful assignment that would make the subsequent loóp “fail” to assign?