Salesforce | System.LimitException: Too many SOQL queries: 101

|
| By Webner

You may face this exception sometimes in Salesforce:

System.LimitException: Too many SOQL queries: 101

There can be multiple reasons for this exception:

Check if SOQL queries are not written in loops
Check if DML are not called inside a loop.
Check if large number of workflows for field update are not created on same object, if possible, move the field update code to trigger.

If none of the above case matches, we can opt for this workaround:

Scenario:
If there are multiple workflows and there is no way to move it to trigger.

Workaround :
Create a class that will hold static Boolean type field for your trigger. Once a trigger is executed, make this flag false. This will ensure that your trigger does not gets executed again and again.

Apex Code:

public Class checkRecursiveCallToTrigger{
	private static boolean run = true;
	public static boolean runOnce(){
	if(run){
 		run=false;
 		return true;
	} else{
    		return run;
	}
	}
}

Now in you trigger set following validation:

trigger ObjectAfterTrigger on Object__c (after delete, after insert, after undelete, after update){

if(checkRecursiveCallToTrigger.runOnce())
    {
	// write you trigger code here
     }
}

Leave a Reply

Your email address will not be published. Required fields are marked *