+ Start a Discussion
vfexp41vfexp41 

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..
AshwaniAshwani


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.

vfexp41vfexp41
@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- https://appexchange.salesforce.com/listingDetail?listingId=a0N30000000qCzXEAU or for more query contact- support@360degreeapps.com.