Triggers and Merge Statements
Merge events do not fire their own trigger events. Instead, they fire delete and update events as follows:
- Deletion of losing records: A single merge operation fires a single delete event for all records that are deleted in the merge. To determine which records were deleted as a result of a merge operation use the
MasterRecordIdfield inTrigger.old. When a record is deleted after losing a merge operation, itsMasterRecordIdfield is set to the ID of the winning record. TheMasterRecordIdfield is only set inafter deletetrigger events. If your application requires special handling for deleted records that occur as a result of a merge, you need to use theafter deletetrigger event. - Update of the winning record: A single merge operation fires a single update event for the winning record only. Any child records that are reparented as a result of the merge operation do not fire triggers.
For example, if two contacts are merged, only the delete and update contact triggers fire. No triggers for records related to the contacts, such as accounts or opportunities, fire.
The following is the order of events when a merge occurs:
- The
before deletetrigger fires. - The system deletes the necessary records due to the merge, assigns new parent records to the child records, and sets the
MasterRecordIdfield on the deleted records. - The
after deletetrigger fires. - The system does the specific updates required for the master record. Normal update triggers apply.