You need to sign in to do that
Don't have an account?
Rodolfo 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
I have been trying to merge duplicates accounts, but I do not know how to make the correct SELECTs queries.
I tried this:
// 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?
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.
Thanks,
Vignesh
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.