Development questions
What does it mean to "Bulkify" an Apex Trigger?
Bulkifying means writing the trigger to handle up to 200 records per invocation (and more through chained chunks) efficiently, without hitting governor limits.
- No SOQL or DML inside
forloops - Collect Ids into Sets, query once, put results in Maps
- Collect records to modify into a List and run one DML after the loop
- Never assume
Trigger.newhas one record
apex
Set<Id> accIds = new Set<Id>();
for (Contact c : Trigger.new) accIds.add(c.AccountId);
Map<Id, Account> accs = new Map<Id, Account>([SELECT Id, Name FROM Account WHERE Id IN :accIds]);
apex-triggertriggerbulkificationbest-practices
More Development questions
- What is an Apex Trigger in Salesforce and when should you use it over Flows?
- What is the difference between before triggers and after triggers?
- Explain all Trigger context variables (isInsert, isUpdate, isDelete, isUndelete, isBefore, isAfter, isExecuting, size, operationType).
- What is the difference between Trigger.new and Trigger.old?
- In which trigger events are Trigger.new and Trigger.newMap available?
- In which trigger events are Trigger.old and Trigger.oldMap available?