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
Rodolfo Calvo 3Rodolfo Calvo 3 

Merge Duplicates into a Master Account

The following code has been taken from https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_merge.htm 
// Create master account
Account master = new Account(Name='Account1');
insert master;

// Create duplicate accounts
Account[] duplicates = new Account[]{
    // Duplicate account 
    new Account(Name='Account1, Inc.'),
    // Second duplicate account
    new Account(Name='Account 1')
};
insert duplicates;

// Create child contact and associate it with first account
Contact c = new Contact(firstname='Joe',lastname='Smith', accountId=duplicates[0].Id);
insert c;


// Merge accounts into master
Database.MergeResult[] results = Database.merge(master, duplicates, false);

for(Database.MergeResult res : results) {
    if (res.isSuccess()) {
        // Get the master ID from the result and validate it
        System.debug('Master record ID: ' + res.getId());
        System.assertEquals(master.Id, res.getId());                
        
        // Get the IDs of the merged records and display them
        List<Id> mergedIds = res.getMergedRecordIds();
        System.debug('IDs of merged records: ' + mergedIds);                
        
        // Get the ID of the reparented record and 
        // validate that this the contact ID.
        System.debug('Reparented record ID: ' + res.getUpdatedRelatedIds());
        System.assertEquals(c.Id, res.getUpdatedRelatedIds()[0]);               
    }
    else {
        for(Database.Error err : res.getErrors()) {
            // Write each error to the debug output
            System.debug(err.getMessage());
        }
    }
}

I have been trying to merge duplicates accounts, but I do not know how to make the correct SELECTs queries. 
I tried this: 
public List<Account> masterAccount;//Account 1
    public List<Account> accountstomerge;//Account 1
    public void MergeTesting()
    {
        //Account masteAcc = new Account(Name = 'Boolean Master');
        Account masteAcc = [Select id, name from Account where name = 'Account 1' Limit 1];
        //insert masteAcc;
        //masterAccount = [SELECT id, name, website, phone, fax, CreatedDate FROM Account WHERE name = 'Account1'];
        accountstomerge = [SELECT id, name, website, phone, fax, CreatedDate FROM Account where name like '%Boolean%'];

        // Merge accounts into master
        
        try
        {
            Database.MergeResult[] results = Database.merge(masteAcc, accountstomerge, false);
            for(Database.MergeResult res : results) {
            if (res.isSuccess()) {
                // Get the master ID from the result and validate it
                System.debug('Master record ID: ' + res.getId());
                System.assertEquals(masteAcc.Id, res.getId());                
                
                // Get the IDs of the merged records and display them
                List<Id> mergedIds = res.getMergedRecordIds();
                System.debug('IDs of merged records: ' + mergedIds);
                
            }
            else {
                for(Database.Error err : res.getErrors()) 
                {
                    //Error message
                }
            }
        }
        }
        catch(Exception e)
        {
            System.debug('The following error: ' + e.getMessage());
        }
But there is no succed. Does anyone know what should be the correct syntax on these scripts to merge accounts successfully?
 
Vignesh P 6Vignesh P 6
Hi Rodolfo,

You can merge up to three records of the same sObject type in UI. For example if you want to merge accounts, go account tab and 'Merge Accounts' in tools section. Then you can merge upto three accounts. User-added image


Thanks,
Vignesh
Rodolfo Calvo 3Rodolfo Calvo 3
Vignesh, thanks! 
The thing is that I need to merge X accounts. So, I know that functionality of salesforce, that's why I am developing an app which allows to merge X quantity of accounts.