Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?

share|improve this question
    
You can create a trigger on Account (before update, before delete) and system debug the account Id to see if the trigger can intercept the merge operation first. –  Bachovski Feb 22 '13 at 3:36
    
Did you come up with a solution for the "Always copy certain fields..." problem? I have been unable to work out the mechanics of saving values in AfterDelete to use in the subsequent BeforeUpdate. –  Chet Seidel Nov 22 '13 at 23:22

1 Answer 1

up vote 5 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.