function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
B TulasiB Tulasi 

Merge Statements in trigger :

Hi All,

Merge Statements in trigger :
1 > What is Merge Statement ?
2 > Why use this merge statement?
3 > When use this merge statement?
Give me one example.
Allready i searched in google. But i am not getting about the winning record and losing record.
can any one explain ?

Thanks in advance
Thulasi
Agustina GarciaAgustina Garcia
Hi
  1. merge is a DML like insert or update but this can only be executed with some Standard objects, Accounts, Contact and Leads. You can merge up to 3 records in one.
  2. This merge statement is used in order to remove duplicate records from your database.
  3. For instance, you have 2 contacts duplicated associated to the same Account. Then you can merge the info of both contacts and remove one of them.
I found this documentation:
  1. merge() -- https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_calls_merge.htm
  2. example -- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_merge.htm
  3. merge duplicate contacts -- https://help.salesforce.com/apex/HTViewHelpDoc?id=contacts_merge.htm&language=nl (https://help.salesforce.com/apex/HTViewHelpDoc?id=contacts_merge.htm&language=nl)
Hope this helps

 
B TulasiB Tulasi
Hi Agustina Garcia,
Thanks you very much.
Agustina GarciaAgustina Garcia
Remember to mark as resolved if this really answer your question, so someone else that have your same doubt can go directly to the solution.
Amit Chaudhary 8Amit Chaudhary 8
Merging Records
When you have duplicate lead, contact, or account records in the database, cleaning up your data and consolidating the records might be a good idea. You can merge up to three records of the same sObject type.

Sample code for you:-
 
// Insert new accounts
List<Account> ls = new List<Account>{
    new Account(name='Acme Inc.'),
        new Account(name='Acme')
        };                                        
insert ls;

// Queries to get the inserted accounts 
Account masterAcct = [SELECT Id, Name FROM Account WHERE Name = 'Acme Inc.' LIMIT 1];
Account mergeAcct = [SELECT Id, Name FROM Account WHERE Name = 'Acme' LIMIT 1];

// Add a contact to the account to be merged
Contact c = new Contact(FirstName='Joe',LastName='Merged');
c.AccountId = mergeAcct.Id;
insert c;

try {
    merge masterAcct mergeAcct;
} catch (DmlException e) {
    // Process exception
    System.debug('An unexpected error has occurred: ' + e.getMessage()); 
}

// Once the account is merged with the master account,
// the related contact should be moved to the master record.
masterAcct = [SELECT Id, Name, (SELECT FirstName,LastName From Contacts) 
              FROM Account WHERE Name = 'Acme Inc.' LIMIT 1];
System.assert(masterAcct.getSObjects('Contacts').size() > 0);
System.assertEquals('Joe', masterAcct.getSObjects('Contacts')[0].get('FirstName'));
System.assertEquals('Merged', masterAcct.getSObjects('Contacts')[0].get('LastName'));

// Verify that the merge record got deleted
Account[] result = [SELECT Id, Name FROM Account WHERE Id=:mergeAcct.Id];
System.assertEquals(0, result.size());
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_merge.htm

1 > What is Merge Statement ?
2 > Why use this merge statement?
3 > When use this merge statement?

When you have duplicate lead, contact, or account records in the database, cleaning up your data and consolidating the records might be a good idea. You can merge up to three records of the same sObject type

1) Only leads, contacts, and accounts can be merged
2) You can pass a master record and up to two additional sObject records to a single merge method
3) External ID fields can’t be used with merge


The following example merges two accounts named 'Acme Inc.' and 'Acme' into a single record:
List<Account> ls = new List<Account>{new Account(name='Acme Inc.'),new Account(name='Acme')};
insert ls;
Account masterAcct = [SELECT Id, Name FROM Account WHERE Name = 'Acme Inc.' LIMIT 1];
Account mergeAcct = [SELECT Id, Name FROM Account WHERE Name = 'Acme' LIMIT 1];
try {
    merge masterAcct mergeAcct;
} catch (DmlException e) {
    // Process exception here
}
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_calls_merge.htm

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 MasterRecordId field in Trigger.old. When a record is deleted after losing a merge operation, its MasterRecordId field is set to the ID of the winning record. The MasterRecordId field is only set inafter delete trigger events. If your application requires special handling for deleted records that occur as a result of a merge, you need to use the after delete trigger 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:
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 theMasterRecordId 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.