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
Tom Ford 15Tom Ford 15 

Exiting a nested flow

Hi,
How can i exit the below Loops? 
For each insertion we need to loop through and assign the line item schedule record ID to each insertion (and vice versa). The code is working but i'm getting errors `Too many SOQL queries: 101` when users try to attibute a large number of insertions. 
The reason for this is to create a backend relationship between an insertion and line item schedule; so if an insertion record is delted so is the associated scheudle.

User-added image
AbhishekAbhishek (Salesforce Developers) 
To fix the issue, change your code so that the number of SOQL fired is less than 100. If you need to change the context, you can use @future annotation which will run the code asynchronously.

https://help.salesforce.com/articleView?id=000181404&type=1&mode=1


I always like to think about future when I write code. I had a similar issue when I started writing Apex a few years ago.

Please look at my advices below:

1. Read this article carefully: http://wiki.developerforce.com/page/Best_Practice%3A_Bulkify_Your_Code
2. Make sure you don't have a recursive loop calling a SOQL.
3. Do not do any DML/CRUD inside a for a loop.
4. Use System.Debug() to ensure what you are fetching is what you should be getting.
5. Make use of the Debug logs/Developer console. Debug it, and you will figure it out.


Look SOQL basically stands for Salesforce Object Query Language (SOQL).SOQL is similar to the SELECT statement in the widely used Structured Query Language (SQL) but is designed specifically for Salesforce data.
For example : SELECT Id, Name FROM Account WHERE Name = 'XYZ'.
So basically whenever you write a query in Salesforce it is counted under SOQL.

And you must be aware of that in Salesforce we are having governor limits.
What are Governor Limits?
As we know, Apex runs in a multi-tenant environment; i.e. a single resource is shared by all the customers and organizations. So, it is necessary to make sure that no one monopolizes the resources and hence Salesforce.com has created the set of limits which governs and limits the code execution. Whenever any of the governor limits are crossed, it will throw error and will halt the execution of program.
From a Developer's perspective, it is of utmost importance to ensure that our code should be scalable and should not hit the limits.
All these limits are applied on per transaction basis. A single trigger execution is one transaction.

Refer: https://developer.salesforce.com/docs/atlas.en-us.salesforce_app_limits_cheatsheet.meta/salesforce_app_limits_cheatsheet/salesforce_app_limits_platform_apexgov.htm

Now coming back to your question : 
You could issue only 100 queries per transaction, that is when your code will issue more than 100 SOQL queries then it will throw an error.
 
Now to avoid this you should follow best practices for writing and designing Apex Code.
One best practice is to Avoid SOQL Queries or DML statements inside FOR Loops because if will write a query inside for loop then after every 100 iterartion the limits will hit and you will get an error 101 stating "Too many SOQL queries: 101".

Refer https://developer.salesforce.com/page/Apex_Code_Best_Practices  for apex best practices.


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
Tom Ford 15Tom Ford 15

Thanks Abhishek , 

I don't have a developers background & all of the links you reffer to are about Apex as opposed to Flow. 

Are you able to provide any examples of how i can exit this particular loop 

AbhishekAbhishek (Salesforce Developers) 
Tom,

While doing the operation you can set the debug logs and will get the details where the error is throwing.

Based on that you will have to perform necessary changes.

It's a custom implementation so there the other examples that didn't work for you.

Please go through the above reasons and suggestions for the Error.

You might need a developer if an issue causing by code.