Order of events when a merge occurs in Salesforce

Order of events when a merge occurs in Salesforce

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.1. The before delete trigger fires.
2. The system deletes the necessary records due to the merge, assigns new parent records to the child records, and sets the MasterRecordId field on the deleted records.
3. The after delete trigger fires.
4. The system does the specific updates required for the master record. Normal update triggers apply.

Account Trigger:

trigger AccountTrigger on Account ( before delete, after delete, before update, after update ) {

if( trigger.isDelete ) {

if ( trigger.isBefore )
system.debug( ‘Before Delete in Account’ );
else if ( trigger.isAfter )
system.debug( ‘After Delete in Account’ );

} else if( trigger.isupdate ) {

if ( trigger.isBefore )
system.debug( ‘Before update in Account’ );
else if ( trigger.isAfter )
system.debug( ‘After update in Account’ );

}

}

Opportunity Trigger:

trigger OpportunityTrigger on Opportunity ( before update, after update ) {

if( trigger.isupdate ) {

if ( trigger.isBefore )
system.debug( ‘Before update in Opportunity’ );
else if ( trigger.isAfter )
system.debug( ‘After update in Opportunity’ );

}

}

Contact Trigger:

trigger ContactTrigger on Contact ( before update, after update ) {

if( trigger.isupdate ) {

if ( trigger.isBefore )
system.debug( ‘Before update in Contact’ );
else if ( trigger.isAfter )
system.debug( ‘After update in Contact’ );

}

}

Output:

Leave a Reply