What does it mean to "Bulkify" an Apex Trigger?
Answer
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