You need to sign in to do that
Don't have an account?
Apex governor limit warning/Debug logs
Hi,
I have been recieving an Apex governor limit warning (Number of SOQL queries: 97 out of 100) after making an update on an Opportunity and decided to look into reasons behind it. Now I am confused and need help.
Here is the deal. We have 3 or 3 different triggers on an Opportunity object, but for now lets concentrate on one. Trigger name is "Update_Split_Quota" and it is after update type trigger. Here is the code (I know it is not idel, it is still work in proggress):
Attached is a small snapshot of the Debug log file showing how my trigger is called and the number of time it is being called

I have been recieving an Apex governor limit warning (Number of SOQL queries: 97 out of 100) after making an update on an Opportunity and decided to look into reasons behind it. Now I am confused and need help.
Here is the deal. We have 3 or 3 different triggers on an Opportunity object, but for now lets concentrate on one. Trigger name is "Update_Split_Quota" and it is after update type trigger. Here is the code (I know it is not idel, it is still work in proggress):
trigger Update_Split_Quota on Opportunity (After Update) { Opportunity[] OppIdNew = Trigger.new; Opportunity[] OppIdOld = Trigger.old; if (Trigger.isAfter){ List<Opportunity> olis = [SELECT Id,AccountId FROM Opportunity WHERE Id IN: Trigger.newMap.keySet()]; for(Opportunity opp: olis){ List<OpportunitySplit> oppsplit = [SELECT Id, OpportunityId, SplitOwnerId, Sales_Quota__c, Legal_Accepted_Date__c FROM OpportunitySplit WHERE OpportunityId = :opp.id]; Account[] account = [Select OwnerId FROM Account Where ID = :opp.AccountID]; if(OppIdNew[0].Order_Type__c=='Services Only'&& OppIdNew[0].StageName == 'Closed Won'){ opp.OwnerId = account[0].OwnerId; //update opp; } for (OpportunitySplit os:oppsplit) { if(os.Legal_Accepted_Date__c != null) { //Only run the trigger if legal accepted date Month_Start = os.Legal_Accepted_Date__c.toStartOfMonth(); //date Month_End = Month_Start.addMonths(1); List<Sales_Quota__c> sales = [SELECT Id, User__C,Month__c, Quarter__c FROM Sales_Quota__c WHERE (User__C = :os.SplitOwnerId) AND (Month__c=:Month_Start) LIMIT 1];//(Quarter__c = THIS_YEAR) AND (User__C = :oppsplit.SplitOwner.id) if(sales.size()!=0) { //for users who do not have quotas Sales_Quota__c s = sales.get(0); os.Sales_Quota__c=s.ID;//Sales_Quota__c = s.ID; update oppsplit; } } } } } }But when I do an update on an Opportunity it does exactly what I want it to do, and I immidiatly get an email with governor limit warning. So I decided to run a debug log to see what is going on, and this is the confusing part. In the log I see that this trigger is being called 6 different time. Each time it is called 3 Select statements inside it are run, and it add up to a lot (18 out 100 possible). My question is WHY DOES IT GET CALLED 6 DIFFERENT TIMES if I only update a single Opportunity (I update an existing Opp, not creating a new one. I simply switch the stage to "Closed Won").
Attached is a small snapshot of the Debug log file showing how my trigger is called and the number of time it is being called
Yes there are about dozen workflows on the Opportunity object, and I would say about half of them are field updates.
If this is the case, is there any way around it?
Thanks.
All Answers
Yes there are about dozen workflows on the Opportunity object, and I would say about half of them are field updates.
If this is the case, is there any way around it?
Thanks.
- Never write multiple trigger for same Object. Solution : Write one trigger but multple helper classes which will be called from the single trigger for various funtionality. This is because if we have multiple trigger, we will not have control over the sequence of triggers.
- Avoid writing business logic in Trigger itself. Solution : The business login should be written inside helper class.
You can find more about Trigger Order of Execution at https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htmbtw your 97 out of 100 is for entire transaction?