Answer: How many times does an Apex trigger execute on a Merge event?
When you perform a merge in Salesforce (merge one record into another), triggers execute based on the role each record plays in the merge. In short:
Key behavior
– The winning (master) record is updated — update triggers run for the master record (before and after update contexts).
– The losing (merged) record(s) are deleted — delete triggers run for each losing record (before and after delete contexts).
How many times?
– If you merge 2 records (1 master + 1 losing), triggers run twice overall: once in delete context for the losing record, and once in update context for the master record.
– If you merge N records (1 master + M losing records), delete triggers will execute for each of the M losing records (M delete executions) and update triggers execute once for the master record (1 update execution). So total trigger invocations across contexts = M (delete) + 1 (update).
Important details and best practices
– Context differences: You will see Trigger.isDelete true for losing records and Trigger.isUpdate true for the master record. There is no Trigger.isMerge flag — you must infer merge behavior by the combination of contexts and record state.
– Child records: During a merge, child records (related lists) may be reparented to the master record; that can fire triggers on those child objects as well. Account/Contact/Lead merges can lead to multiple other triggers firing because of reparenting or updates to related data.
– Bulk and recursion: Write merge-aware, bulkified triggers. Separate logic by trigger context (isDelete, isUpdate) to avoid double-processing. Use static variables or a trigger framework to prevent recursion and to ensure idempotent behavior.
Example pattern
Use separate handler methods for delete vs update contexts so your logic handles only the intended records:
if (Trigger.isBefore && Trigger.isDelete) {
// Logic for losing records being deleted (merge losers)
}
if (Trigger.isAfter && Trigger.isUpdate) {
// Logic for the master record after merge (updated fields)
}
Summary: There isn’t a single fixed count independent of how many records are merged. Triggers run for each losing record in delete context and once for the master record in update context — e.g., merging 3 records into 1 causes 2 delete-trigger executions and 1 update-trigger execution.








Leave a Reply