Handle Governor Limits
By the end of this lesson, you'll be able to:
- Verify the trigger handler stays within SOQL and DML limits at realistic batch sizes
- Recognize any remaining risk points in the handler as written
- Confirm bulk safety by reasoning through a 200-record scenario
Prerequisites: "Add Validation in Apex"
Walking through a 200-record update
// A bulk update: 200 Opportunities all moving to Closed Won at once
List<Opportunity> opportunities = [SELECT Id FROM Opportunity WHERE StageName != 'Closed Won' LIMIT 200];
for (Opportunity opp : opportunities) {
opp.StageName = 'Closed Won';
}
update opportunities; // fires OpportunityTrigger with all 200 records at once
// A bulk update: 200 Opportunities all moving to Closed Won at once— the scenario being traced through.List<Opportunity> opportunities = [SELECT Id FROM Opportunity WHERE StageName != 'Closed Won' LIMIT 200];— one query, fetching the maximum realistic batch.for (Opportunity opp : opportunities) {— loops over them, in memory.opp.StageName = 'Closed Won';— changes the field.}— closes the loop.update opportunities;— firesOpportunityTriggerwith all 200 records at once. Tracing throughafterUpdatefor this batch: theforloop over 200 records runs zero SOQL and zero DML — it only buildsamountAdjustmentByAccountIdin memory (Module 25's "collect" half).applyAdjustmentsthen runs exactly one query and oneupdate, regardless of whether those 200 Opportunities point to 200 different Accounts or all share the same one.
Confirming there's no hidden per-record operation
Scanning beforeUpdate and afterUpdate for anything that could scale with record count: the for loops themselves are pure in-memory work (no SOQL/DML inside them), addError() doesn't count as DML, and both applyAdjustments's query and update run exactly once regardless of batch size. This handler is bulk-safe as written — genuinely satisfying Module 25's core requirement, not just appearing to.
A remaining risk worth naming: recursion
// applyAdjustments updates Account — if an AccountTrigger existed
// that itself updated related Opportunities, this could chain back
// into OpportunityTrigger again.
// applyAdjustments updates Account — if an AccountTrigger existed—applyAdjustments'supdate accountsis itself a DML statement.// that itself updated related Opportunities, this could chain back— that could, in a more complex org with anAccountTriggerof its own, trigger a chain of further automation.// into OpportunityTrigger again.— this is exactly Module 24's trigger recursion concern, worth being aware of even though this specific module's simple version doesn't need a recursion guard yet.
Exercise
As a comment, confirm whether this handler's beforeUpdate method (validation only) is bulk-safe, and explain why or why not.
Show hint
Look back at the beforeUpdate method from the previous lesson.
Handle Governor Limits Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
This lesson pressure-tests the handler built so far against Module 25's governor limits — walking through exactly what happens with the maximum realistic trigger batch size.