Development questions

What does it mean to "Bulkify" an Apex Trigger?

DevelopmentEasyTCSAccentureDeloitte

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 for loops
  • 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.new has 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