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
Steve Cox 9Steve Cox 9 

Help rewriting flowe to avoid a 'Too many SOQL queries: 101' error

I have some flows that are causing too many SOQL errors.

I have a flow that for any contact ID, will:
- lookup all related records (m:m) in a custom module (entitlements); 
- constuct a text field from fields from all these records;
- add this text field to the contact record and write to the database.

(the text feld is used in calling external APIs)

That works fine. However if an entitlement record is edited, then all contacts that refer to this entitlement need to have the text field re-constructed.

I created a second visual flow that for a entitlement record:
- lookups all related contacts
- for each contact, call the first flow.

Now, using the second flow, it can end up looping through a large number of contacts and call loops for each that hit the database - causing the 'to many' error.

I can't think of an easy way to rewrite these flows so I'm hoping for some suggestions. I have very limited experience with Apex development so I'm hoping I can keep the work using flows and process manager etc.

As it looks like I need to break this into different contexts, is there a way to have the first flow schedule the looped calls to the second flow - in a way similar to using @future? So that they don't all occur at once in the same context?

Thanks
Parker EdelmannParker Edelmann
Can you post a screenshot of your flow? Knowing the layout might help.
Steve Cox 9Steve Cox 9
Thanks for the reply. Sorry for the delay in replying. I've attached the two screenshots. The first is the main flow that is called against a contact. The first lookup creates and object collection of 'subs_users' (I've referred to these as entitlements previously). These are the intermediary relationship objects that link contacts m:m to subscriptions.

Next the loop goes through these: looks up the related subscription; uses a decision to check if the subscription is active (a status field in the subscription object) and if it is, adds the subscription to a collection.

When that loop is completed, a new loop goes through these active subscription objects, creating a concatenated string of distinct subscription titles. Once completed, this string is written to a field in the contact record.

User-added image
Steve Cox 9Steve Cox 9
This is the other flow. If an actual subscription record is altered, then all the related contacts need to be updated. 

The flow looks up all the subs_users objects related to the subscription (the subs-users was the m:m relationship object). The flow then loops through these to process the contact IDs and use these to call the main flow above.

User-added image

This is the main cause of the 'too many queries', as it's creating a loop within a loop and generating multiple queries.

If it's not possible to solve this using visual flows, what would be the option? Also, is there an intermeidate work-around that would allow the user to save an updated subscription and not recieve this error - but instead get something like 'flow abandoned but record saved' message?

Many thanks