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
Paras JainParas Jain 

I am getting sometimes error when submit a page System.LimitException: Too many SOQL queries: 101

System.LimitException: Too many SOQL queries: 101
Error is in expression '{!saveclosedwonpage}' in component <apex:commandButton> in page nonapaclosedwonform: Trigger.InvestmentCriteriaUpdate: line 29, column 1

Trigger.InvestmentCriteriaUpdate: line 29, column 1

there are around 11 triggers, as i have checked in debug log, and they fires 3 to 4 times...and which makes SOQL query increasing
Also the workflows are fire as i checked in debug... what approach should i have to folllow to resolve this problem....


Amit Chaudhary 8Amit Chaudhary 8
Hi Paras, 

Following issue you have in your code:-
1) Always create only one trigger per object as per salesforce best pratice
2) Recursive trigger

Solution 1:- how to stop recursive trigger in salesforce
http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html

 
Apex Class with Static Variable
public class ContactTriggerHandler
{
     public static Boolean isFirstTime = true;
}

Trigger Code
trigger ContactTriggers on Contact (after update)
{
    Set<String> accIdSet = new Set<String>(); 
    if(ContactTriggerHandler.isFirstTime)
    {
        ContactTriggerHandler.isFirstTime = false;
         for(Contact conObj : Trigger.New)
  {
            if(conObj.name != 'Test') 
     {
                accIdSet.add(conObj.accountId);
            }
         }
   // any code here
    }
}

Solution 2:- Use salesforce frame work
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Create one Trigger "AccountTrigger"
trigger AccountTrigger on Account( after insert, after update, before insert, before update)
{

    AccountTriggerHandler handler = new AccountTriggerHandler(Trigger.isExecuting, Trigger.size);
    
    if( Trigger.isInsert )
    {
        if(Trigger.isBefore)
        {
            handler.OnBeforeInsert(trigger.New);
        }
        else
        {
            handler.OnAfterInsert(trigger.New);
        }
    }
    else if ( Trigger.isUpdate )
    {
        if(Trigger.isBefore)
        {
            handler.OnBeforeUpdate(trigger.New ,trigger.Old,Trigger.NewMap,Trigger.OldMap);
        }
        else
        {
            handler.OnAfterUpdate(trigger.New ,trigger.Old,Trigger.NewMap,Trigger.OldMap);
        }
    }
}

Create one Trigger Handler Class
public with sharing class AccountTriggerHandler 
{
    private boolean m_isExecuting = false;
    private integer BatchSize = 0;
    public static boolean IsFromBachJob ;
    public static boolean isFromUploadAPI=false;
    
    public AccountTriggerHandler(boolean isExecuting, integer size)
    {
        m_isExecuting = isExecuting;
        BatchSize = size;
    }
            

    public void OnBeforeInsert(List<Account> newAccount)
    {
        system.debug('Account Trigger On Before Insert');
    }
    public void OnAfterInsert(List<Account> newAccount)
    {
        system.debug('Account Trigger On After Insert');
    }
    public void OnAfterUpdate( List<Account> newAccount, List<Account> oldAccount, Map<ID, Account> newAccountMap , Map<ID, Account> oldAccountMap )
    {
        system.debug('Account Trigger On After Update ');
        AccountActions.updateContact (newAccount);
    }
    public void OnBeforeUpdate( List<Account> newAccount, List<Account> oldAccount, Map<ID, Account> newAccountMap , Map<ID, Account> oldAccountMap )
    {
        system.debug('Account Trigger On Before Update ');
    }

    @future 
    public static void OnAfterUpdateAsync(Set<ID> newAccountIDs)
    {

    }      
    public boolean IsTriggerContext
    {
        get{ return m_isExecuting;}
    }
    
    public boolean IsVisualforcePageContext
    {
        get{ return !IsTriggerContext;}
    }
    
    public boolean IsWebServiceContext
    {
        get{ return !IsTriggerContext;}
    }
    
    public boolean IsExecuteAnonymousContext
    {
        get{ return !IsTriggerContext;}
    }
}

Create one Trigger Action Class
 
public without sharing class AccountActions 
{
    public static void updateContact ( List<Account> newAccount)
    {
        // Add your logic here
    }
}

Please mark this as solution if this will help you. So that is some one has same issue this post can help others

Thanks
Amit Chaudhary
Amit Chaudhary 8Amit Chaudhary 8
Please let us know if above solution helps you.