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

Merge accounts

Hi All,

                I have a two Accounts----that is Account A
                                                                         Account B

         For my Account A have 5 contacts,
         For my Account B have 3 contacts    my goal is i have to merge Account A to Account B in this i have to assign the Account A contacts to Account B, without removing contacts from Account A.. How can i start my work to achieve this functionality ..any suggestions..

There is a DML operation "merge"

Look at following example:

// 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()); 

Deepak Kumar ShyoranDeepak Kumar Shyoran
For you need to use Merge DML
merge TargetObject  SourceObject ;
you can define only two record at a time in Merge DML first one on which you want to merge the record and the second which you want to merge.

@deepak kumar, @Avilion can you give some trigger for this concept..
John Wilson 59John Wilson 59
By using 360 Merge Duplicate App you can merge two accounts into the single account and while merging above two accounts A and b there will be total of the eight contacts, which will associate with single account. For more information take a look at the link- or for more query contact-