# Help Needed: Troubleshooting Workflow Loops for Lead Distribution

**URL:** <https://community.glideapps.com/t/help-needed-troubleshooting-workflow-loops-for-lead-distribution/79530>\
**Category:** Ask for Help\
**Tags:** workflows\
**Created:** [January 25, 2025, 11:15pm UTC](https://community.glideapps.com/t/help-needed-troubleshooting-workflow-loops-for-lead-distribution/79530 "2025-01-25T23:15:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor\_Ramos](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/victor_ramos/32/82084_2.png) [@Victor\_Ramos](https://community.glideapps.com/u/Victor_Ramos)\
**Post date:** [January 25, 2025, 11:15pm UTC](https://community.glideapps.com/t/help-needed-troubleshooting-workflow-loops-for-lead-distribution/79530/1 "2025-01-25T23:15:08Z")

</div>

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:**  
 ![Screenshot 2025-01-25 at 3.09.28 PM](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/1/7/171791a4571b1166c7024797b46763146e27284d.png)

 ![Screenshot 2025-01-25 at 3.09.43 PM](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/0/c/0c46515444009e5caa3dbbadc682ad18a89477b1.png)

1. **Logs:**

• Shows how cumulative math is calculated correctly.

 ![Screenshot 2025-01-25 at 3.09.58 PM](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/2/b/2b985ed510096a8cb864906e555a3db1cea4ef9d.png)

• 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. 🙏

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [January 26, 2025, 12:41am UTC](https://community.glideapps.com/t/help-needed-troubleshooting-workflow-loops-for-lead-distribution/79530/2 "2025-01-26T00:41:12Z")

</div>

> [@Victor\_Ramos](#):
>
> How to **exit a loop** or **skip iterations** after a condition is met (e.g., lead is already assigned).

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?

---

<div class="post-metadata">

**Author:** ![Victor\_Ramos](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/victor_ramos/32/82084_2.png) [@Victor\_Ramos](https://community.glideapps.com/u/Victor_Ramos)\
**Post date:** [January 26, 2025, 5:27pm UTC](https://community.glideapps.com/t/help-needed-troubleshooting-workflow-loops-for-lead-distribution/79530/3 "2025-01-26T17:27:52Z")

</div>

> [@ThinhDinh](#):
>
> 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!

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [January 26, 2025, 11:54pm UTC](https://community.glideapps.com/t/help-needed-troubleshooting-workflow-loops-for-lead-distribution/79530/4 "2025-01-26T23:54:59Z")

</div>

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?
