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
njo103njo103 

Soql governor limit since winter 16

Hello,


Since winter 16, we are encountering a Salesforce governor limit of 100 soql queries.  When looking at the debug log, the following happens :

- Trigger on object that updates the records
- Workflows are triggered
- Trigger on updated records are triggered following workflow updates

However, in this case, for each upated record by the workflow, considering 5 records :

workflow update on 1 record =>trigger is triggered and soql counted
workflow update on 1 record =>trigger is triggered and soql cumulated
 on so on for all 5 records

This has become a blocking issue.

Could you please help?  Has there been a change in the way Salesforce counts the governor limits?

Thanks,
Nive
pconpcon
This is not a new limit and is likely that you have added something to your process that is causing this error.  I would recommend that you change your log levels to:
  • DB: DEBUG
  • Callouts: NONE
  • ApexCode: INFO
  • Validation: NONE
  • Workflow: NONE
  • Profiling: NONE
  • Visualforce: NONE
  • System: INFO
Then see where you are calling your SOQL queries and are making more than the alloted amount.  This typically happens when you have SOQL inside of a loop.  If you cannot figure out where your code is going off of the rails, feel free to include the code here and we can try to help you.

NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
njo103njo103
Indeed, it's not a new governor limit. Actually, this governor limit was not exceeded before and no changes has been done to the code. And workflows not added. I wanted to confirm whether it's normal that the counting of soql queries should be cumulated for each record that is updated via workflow.
pconpcon
The limits run for the entire transaction so if you have workflows that update object and those objects have triggers on them, then the SOQL queries are cumulative.  As stated before, if you are hitting the 100 SOQL limit it sounds like you have some non-optimized trigger code
njo103njo103
Hello,

The developpement is indeed a very old developement that could certainly be optimized but so far it worked well with bulk.  

I understand that the soql queries are cumulative but are they cumulated per record updated by the workflow field updates?

Does that mean that if my trigger size is 500 records and worfklows are triggered for those 500 records that in turn would trigger my triggers on that particular object, the governor limits would be cumulated per record following the worfklow update per record?
pconpcon
The limtis are cumulative for the entire life of the transaction across all objects.  So for example, if you had the following trigger:
trigger LeadTrigger on Lead (before insert, before update) {
     for (Lead l : Trigger.new) {
          User u = [
               select Name
               from User
               where Id = :l.OwnerId
          ];

          l.Owner_Name__c = u.Name;
     }
}
I know this is a silly trigger but it's here to serve a point

If you did a dataload that inserted 20 leads the flow would go
  • before insert trigger fires (20 SOQL queries)
  • workflows fire (workflow updates an unrelated field on the Lead)
  • before update triger fires (20 SOQL queries)
At the end of this cycle we've used a total of 40 SOQL queries.

Where we've run into problems with older code that has not bulk ready is that for a long time it'll run fine for a long time (even with bulk operations) but then we'll add a single new trigger or workflow (or even the data changes slightly) and where we were hovering at 98 queries now we've been pushed over the threshold to 100+ queries.

Where I would start to diagnose the issue is to change your log levels in the debug log, do your operation and then look through the logs to see where your SOQL governor limits suddenly spike.  Then look at that trigger and see if you can make it bulk ready.  Then repeat until your SOQL limits are low enough for comfort.  Then put in place a plan to review all old triggers and make then bulk ready.  Possibly look at implementing a trigger framework [1] and using lazy loading [2] to reduce some of your total SOQL queries.

[1] http://blog.deadlypenguin.com/blog/2012/02/13/classifying-triggers-in-salesforce/
[2] http://blog.deadlypenguin.com/blog/2012/04/04/reducing-salesforce-soql-queries-by-using-static-variables/
njo103njo103
The trigger is already in bulk.  In your example, it's not bulk.  So i understand that the field updates are cumulated per record updated.  But if the trigger is already in bulk, triggers triggered by field updates should not be cumulated PER RECORD updated through field updates right?

Please find below part of the debug log :

As you can see the afterUpdate on the opportunity updates existing tasks on the opportunity and the update has been done for a list of tasks.
However, the workflow that is then triggered on the tasks cumulates the queries PER record.  If i have 50 tasks, then am already close to my SOQL limit.

I do not understand why this is being cumlated per record.
11:18:57.859 (859508163)|CODE_UNIT_FINISHED|OpportunityAfterUpdate on Opportunity trigger event AfterUpdate for [006D000000XzKwo]
11:18:57.870 (870747347)|CUMULATIVE_LIMIT_USAGE
11:18:57.870 (870747347)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 14 out of 100
  Number of query rows: 23 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 2 out of 150
  Number of DML rows: 41 out of 10000
  Maximum CPU time: 146 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

11:18:57.870 (870747347)|CUMULATIVE_LIMIT_USAGE_END

11:18:57.870 (870970021)|CODE_UNIT_FINISHED|TaskAfterUpdate on Task trigger event AfterUpdate for [00T5700003A8WyN, 00T5700003Bd4ee, 00T5700003BtW0y, 00T5700003Ck4ob, 00T5700003ClXNY, 00T5700003DfYNe, 00T5700003F8aDx, 00T5700003F8aqc, 00T5700003F8aqw, 00T5700003F8as8, 00T5700003F9GXK, 00T5700003F9qlZ, 00T5700003F9uKB, 00T5700003Fnzw2, 00TD000001xNwRR, 00TD000001yGEPU, 00TD000002KvtaJ, 00TD000002TAeCV, 00TD000002iMrqa, 00TD000002iPWVH, 00TD000002oxpRA, 00TD000002oxpRB, 00TD000002puE65, 00TD000002puELG, 00TD000002puMzq, 00TD000002puN0y, 00TD000002srT15, 00TD000002suHFI, 00TD000002suHFJ, 00TD000002uMLpG, 00TD000002vwzeI, 00TD0000033IBiO, 00TD0000037PSMF, 00TD0000039C1Ku, 00TD0000039C1N0]
11:18:57.871 (871099283)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Task
11:18:57.891 (891641029)|CODE_UNIT_FINISHED|Workflow:Task
11:18:57.893 (893426310)|CODE_UNIT_STARTED|[EXTERNAL]|01q20000000GrmN|TaskBeforeUpdate on Task trigger event BeforeUpdate for [00TD000001yGEev]
11:18:57.902 (902781296)|CUMULATIVE_LIMIT_USAGE
11:18:57.902 (902781296)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 17 out of 100
  Number of query rows: 25 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 2 out of 150
  Number of DML rows: 41 out of 10000
  Maximum CPU time: 146 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

11:18:57.902 (902781296)|CUMULATIVE_LIMIT_USAGE_END

11:18:57.903 (903081848)|CODE_UNIT_FINISHED|TaskBeforeUpdate on Task trigger event BeforeUpdate for [00TD000001yGEev]
11:18:57.921 (921134196)|CODE_UNIT_STARTED|[EXTERNAL]|01q20000000GqhA|TaskAfterUpdate on Task trigger event AfterUpdate for [00TD000001yGEev]
11:18:57.929 (929847806)|CUMULATIVE_LIMIT_USAGE
11:18:57.929 (929847806)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 19 out of 100
  Number of query rows: 26 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 2 out of 150
  Number of DML rows: 41 out of 10000
  Maximum CPU time: 146 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

11:18:57.929 (929847806)|CUMULATIVE_LIMIT_USAGE_END

11:18:57.930 (930148894)|CODE_UNIT_FINISHED|TaskAfterUpdate on Task trigger event AfterUpdate for [00TD000001yGEev]
11:18:57.930 (930920685)|CODE_UNIT_STARTED|[EXTERNAL]|01q20000000GrmN|TaskBeforeUpdate on Task trigger event BeforeUpdate for [00TD000002KvtcU]
11:18:57.941 (941448873)|CUMULATIVE_LIMIT_USAGE
11:18:57.941 (941448873)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 22 out of 100
  Number of query rows: 28 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 2 out of 150
  Number of DML rows: 41 out of 10000
  Maximum CPU time: 146 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

11:18:57.941 (941448873)|CUMULATIVE_LIMIT_USAGE_END

11:18:57.941 (941907854)|CODE_UNIT_FINISHED|TaskBeforeUpdate on Task trigger event BeforeUpdate for [00TD000002KvtcU]
11:18:57.965 (965226371)|CODE_UNIT_STARTED|[EXTERNAL]|01q20000000GqhA|TaskAfterUpdate on Task trigger event AfterUpdate for [00TD000002KvtcU]
11:18:57.973 (973538869)|CUMULATIVE_LIMIT_USAGE
11:18:57.973 (973538869)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 24 out of 100

 
 
njo103njo103
I've tried to reproduce this on my developer org but the soql is not cumulated per record.
 
04:34:55.152 (152088171)|CODE_UNIT_STARTED|[EXTERNAL]|01qd0000000sZde|OpportunityAfterUpdate on Opportunity trigger event AfterUpdate for [006d0000005zRtX]
04:34:55.155 (155789801)|SOQL_EXECUTE_BEGIN|[3]|Aggregations:0|SELECT Id FROM task WHERE WhatId IN :tmpVar1
04:34:55.162 (162446749)|SOQL_EXECUTE_END|[3]|Rows:3
04:34:55.162 (162648713)|DML_BEGIN|[3]|Op:Update|Type:Task|Rows:3
04:34:55.173 (173971994)|CODE_UNIT_STARTED|[EXTERNAL]|01qd0000000sZdj|TaskBeforeUpdate on Task trigger event BeforeUpdate for [00Td000002KB2Zn, 00Td000002KB2dL, 00Td000002KB2sQ]
04:34:55.174 (174474261)|SOQL_EXECUTE_BEGIN|[3]|Aggregations:0|SELECT Id FROM Opportunity limit1
04:34:55.176 (176452848)|SOQL_EXECUTE_END|[3]|Rows:33
04:34:55.176 (176648333)|CUMULATIVE_LIMIT_USAGE
04:34:55.176 (176648333)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 2 out of 100
  Number of query rows: 36 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 3 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

04:34:55.176 (176648333)|CUMULATIVE_LIMIT_USAGE_END

04:34:55.176 (176847658)|CODE_UNIT_FINISHED|TaskBeforeUpdate on Task trigger event BeforeUpdate for [00Td000002KB2Zn, 00Td000002KB2dL, 00Td000002KB2sQ]
04:34:55.202 (202945438)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Task
04:34:55.230 (230499203)|CODE_UNIT_STARTED|[EXTERNAL]|01qd0000000sZdj|TaskBeforeUpdate on Task trigger event BeforeUpdate for [00Td000002KB2Zn, 00Td000002KB2dL, 00Td000002KB2sQ]
04:34:55.230 (230814722)|SOQL_EXECUTE_BEGIN|[3]|Aggregations:0|SELECT Id FROM Opportunity limit1
04:34:55.232 (232716489)|SOQL_EXECUTE_END|[3]|Rows:33
04:34:55.232 (232893648)|CUMULATIVE_LIMIT_USAGE
04:34:55.232 (232893648)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 3 out of 100
  Number of query rows: 69 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 3 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

04:34:55.232 (232893648)|CUMULATIVE_LIMIT_USAGE_END

04:34:55.233 (233139494)|CODE_UNIT_FINISHED|TaskBeforeUpdate on Task trigger event BeforeUpdate for [00Td000002KB2Zn, 00Td000002KB2dL, 00Td000002KB2sQ]
04:34:55.251 (251733830)|CODE_UNIT_FINISHED|Workflow:Task
04:34:55.252 (252013808)|DML_END|[3]
04:34:55.252 (252167407)|CUMULATIVE_LIMIT_USAGE
04:34:55.252 (252167407)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 3 out of 100

 
pconpcon
I do not see in your debug log how it is "PER record."  I do see what appears to be a Workflow on your Task object that appears to create a new task that then re-fires your triggers (thus recalling your SOQL).  If you look at line 40 of your first debug log, you'll see that the Id of 00TD000001yGEev first occurs there, meaning that it is being created after your Task workflow.  What are the criteria surrounding Task workflows in your organiztion?  When was the last time your sandbox was updated from your Production instance?  Are you working with the same data in your sandbox that you are in your production instance?
njo103njo103
Hello,

Thank you for your help. 

But when trying to regenerate a debug log to check what was trigger the updates per record, the issue no longer existed and the update got through without errors.