Programmatic capabilities for reacting to Account Merges

Who has dealt with merges in triggers? I know a merge operation (specifically Account merges) fires a delete trigger on the loser, and an update on the winner. To what degree can I, programmatically, detect or interfere with this process? Can I:

React to the content of the record being deleted, and prevent the merge from happening?

Always copy certain fields from the loser to the winner, no matter what the user selects?

Inspect the content of the winner AND the loser, and through some logic, decide to let the merge go through or not?

Within the delete or update triggers, differentiate between a regular update or delete, and one that is fired as a result of a merge?

Answer

In a previous life, I wrote a trigger to prevent merge operations in certain cases – for example, if the “loser” was a verified account, and the “winner” was unverified, an error would occur. The MasterRecordID is important here – IIRC, on a losing record it will point to the winning record (it’ll be blank for the winner)

There’s a page in the Apex docs called “Triggers and Merge Statements” that should help shed some light.

To address your specific questions:

React to the content of the record being deleted, and prevent the
merge from happening?

Yes, merge operations cause triggers on Account to fire, as they are edit/delete events. You can addError() records to prevent it.

Always copy certain fields from the loser to the winner, no matter
what the user selects?

I think this should be possible, though you may need to store some information in static variables depending on the order of events. The delete(s) take place and then the update, so you need something to hold the value between the triggers. I could be wrong on this. Should be easy to write a trigger with the necessary events to test this out.

Inspect the content of the winner AND the loser, and through some
logic, decide to let the merge go through or not?

Yep, this is what we did, as alluded to above.

Attribution
Source : Link , Question Author : Matthew Lamb , Answer Author : Jason Clark

Leave a Comment