function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LaraWasowskiLaraWasowski 

Flow failing when more than 100 records are passed in

I have a flow that is designed to create a new donation for each campaign member (contact) on a campaign.  The flow is triggered by a button on the campaign.  The flow works fine for low volume, but when I click the button and there are 100+ contacts on the campaign, it fails, with a "too many SOQL queries" error. 

I am using FastLookup / FastUpdate / FastCreate, and was under the impression that the flow functionality was by design able to handle large volumes.  Advice?  Tips? 
User-added image
Best Answer chosen by LaraWasowski
Gaurav HandooGaurav Handoo
Hi Lara

You seem to be using Fast Lookup within Loop element, which would definitely hit your SOQL limit. Would recommend moving the same out of the loop. You could do the following:
  1. Fetch the Campaign Member Details (For Lead and Contact) in the Fast Lookup (you might need to add generic Fast lookup before the loop).
  2. Inside the loop add another Decision Box to compare the CovertedContactId and the Contact Id fetched (This is an assumption that you are re-fetching campaign members if lead is converted without opportunity).
  3. Based on your decision box (inside the loop), directly move to assignment (you should already be having the values in SObject Variable of loop).
Hope this helps.

Cheers!!

Gaurav

All Answers

Gaurav HandooGaurav Handoo
Hi Lara

You seem to be using Fast Lookup within Loop element, which would definitely hit your SOQL limit. Would recommend moving the same out of the loop. You could do the following:
  1. Fetch the Campaign Member Details (For Lead and Contact) in the Fast Lookup (you might need to add generic Fast lookup before the loop).
  2. Inside the loop add another Decision Box to compare the CovertedContactId and the Contact Id fetched (This is an assumption that you are re-fetching campaign members if lead is converted without opportunity).
  3. Based on your decision box (inside the loop), directly move to assignment (you should already be having the values in SObject Variable of loop).
Hope this helps.

Cheers!!

Gaurav
This was selected as the best answer
LaraWasowskiLaraWasowski
Thanks Gaurav!!  Moving that lookup outside did the trick...

But now I am up against the "Maximum number of executed elements at run time" limit of 2000.  I can do my best to cut down on the operations inside the loop, but does anyone have experience with other ways of getting around this limit if you want to be able to pass in more thank 2000 records into a loop?
Gaurav HandooGaurav Handoo
Hi Lara

It seems to be the 'Maximum number of executed elements at runtime' limit that I am hitting (the limit is 2000, see the Salesforce documentation here: https://help.salesforce.com/apex/HTViewHelpDoc?id=vpm_admin_flow_limits.htm).
The reason this limit is hit is that when looping through records, each element in the loop counts as an executed element multiple times (once per record that you loop through).
For example, the simple loop below could process a maximum of 666 records: 
the 3 elements in the loop * 666 = 1998 executed elements 
+ fast lookup element = 1999 executed elements 
+ fast update element = 2000 executed elements - the limit

User-added image


If you have additional elements inside your loop, the number of records you can process will be reduced accordingly. For example if there were four elements in my example loop, then the flow could only process (2000 - 2) / 4 = 499 records before hitting the limit. I believe this is the limit you're hitting, as you say that it's adding extra elements that is reducing the number of records you can process.

Would recommend, merging your elements wherever possible to ensure you have minimum number of elements to be executed in a single path.

Hope this helps.

Cheers!!

Gaurav
LaraWasowskiLaraWasowski
again - thanks so much.  I do understand why I am hitting the limits, but now I'm mostly curious as to ways of getting around this - if I have e.g. 2500 records for which I want to create opportunities - is there a way of designing the flow so that it runs through the 2000 element limit (let's say I have 2 elements in the loop; the first run will then take 500 records) and then calls itself again with the next set of records?
Gaurav HandooGaurav Handoo
Hi Lara

When you are calling a flow from button/process builder and processing records being fetched within a flow, breaking it into batches is not feasible (wasn't albe to achieve that). However, there are two ways to achieve this:
  1. Try to merge/generalise most of the assignments and decision boxes that you are using. This should reduce your number of executable elements within a flow.
  2. Break your Flow into two parts
    1. One segment would process the records and create one set of data.
    2. Pass that data into an Apex Class, do the further processing there (preferrably the loop part) and return back to Flow with processed information.
Hope this helps.

Cheers!!

Gaurav