• Rodolfo Calvo 3
  • NEWBIE
  • 20 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 6
    Replies
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?
 
I have the following code: 
Account master = new Account(Name='Account1')
insert master;

But this is for inserting a new account. How can I get an existing account with the same structure?
Something like: 
Account acc = account(Where name = :myVariable);
Hello team, 

I am developing a merging app but I cannot find to way to merge what I need. A have a multiple selection for all the accounts I want to merge and the Master Account. 
Here is the code: 
 
public class MyAccountListCntrlr {
    // PROPERTIES
    public List<AccountWrapperCls> acctList {get;set;}
    public Set<String> selAccountNames {get;set;}
    public Boolean hasSelAcct {get;set;}
    public static List<Account> accts;
    
    //	MESSAGE
    public static  Boolean showMessage {get;set;}
	public static String message {get;set;}
    
    public static List<Account> acct;//Account 1
    public static List<Account> acct2;//Account 2
    
    
    public static String sAccount{set;get;}//Selection of the Master Account
    //See the method getItems
    
    
     // CONSTRUCTOR
     public MyAccountListCntrlr(){
          acctList = new List<AccountWrapperCls>();
          selAccountNames = new Set<String>();
         showMessage = false;
          for(Account a : [SELECT AccountNumber, CreatedDate, name, id, phone, fax, website, type, industry, description, NumberOfEmployees, BillingCity, AnnualRevenue from account
          ORDER BY Account.Name]){
               acctList.add(new AccountWrapperCls(a));
          }
     }
    
    // SELECTING MASTER
    public List<SelectOption> getItems() 
    {
        List<Account> lstAcc = [select id, name from account ORDER BY Account.Name];
        List<SelectOption> options = new List<SelectOption>();
        
        for(Account acc : lstAcc)
        {
            options.add( new SelectOption( acc.id, acc.Name));
            options.sort();
        }
        return options;
    }
    
    
    
   public Account masterAcc = new account(name = sAccount);
    
    
     // METHODS
     public void displaySelectedAccountNumbers(){
          //selAccountNames.clear();
          //masterAcc = [Select name from Account where name = :sAccount];
          hasSelAcct = false;
          for(AccountWrapperCls cWrapper : acctList){
               if(cWrapper.isSelected){
                    hasSelAcct = true;
                    selAccountNames.add(cWrapper.cAccount.name);
                   
                   	//showMessage = true;
                   	//message = 'The following error has ocurred: \n' + cWrapper.cAccount.id;
                   	//accts.add(cWrapper.cAccount);
               }
               if(selAccountNames.size() > 1)
                {
                    for(Integer i = 1; i < selAccountNames.size(); i++)
                    {
                        showMessage = true;
                        message = 'The selected accounts are: \n' + selAccountNames;
                        acct = [Select id, name, phone FROM Account where id = :selAccountNames];
                        //acct2 = [Select id, name, phone FROM Account where id = :selAccountNames ORDER BY Account.CreatedDate];
                        
						
                        message = 'The selected query is: \n' + acct;
                        if(selAccountNames.size() > 1)
                        {
                         	try
                            {	
                                //acct.get(i).description = '\nAccount Name: ' + acct.get(0).Name;
                                Database.MergeResult[] results = Database.merge(masterAcc, acct, false);
                                
                                //merge acct.get(0) acct2.get(i); 
                                //merge acct2.get(i) master.get(1);
                                //If the first Account was created first than 2nd one then do not merge
                                /*if(acct.get(0).CreatedDate < acct.get(i).CreatedDate)
                                {
                                    acct.get(0).description = '\nAccount Name: ' + acct.get(i).Name;
                                    merge acct.get(i) acct2.get(0);     
                                }
                               else
                                {
                                    acct.get(i).description = '\nAccount Name: ' + acct.get(0).Name;
                                    merge acct.get(0) acct2.get(i); 
                                }*/
                            }
                            catch(Exception e)
                            {
                                showMessage = true;
                                message = 'The following error has founded: ' + e;
                            }
                        }
                    }
                    
                    showMessage = true;
                    message = 'Congratulations, you have merge successfully your accounts, please refresh the site to see changes. ';
                }
          }
     }
    
    public PageReference PreviousPag()
    {
        return Page.LCA;
    }    
}

Can somebody help me out with this issue?
Thanks in advace. 
 
I have an app to show iteratively the IDs by selecting a checkBox. I need to merge the selected accounts, how can I do that?
This is the Controller:
 
public class MyAccountListCntrlr {
// PROPERTIES
public List<AccountWrapperCls> acctList {get;set;}
public Set<String> selAccountNames {get;set;}
public Boolean hasSelAcct {get;set;}
public static List<Account> accts;

//  MESSAGE
public static  Boolean showMessage {get;set;}
public static String message {get;set;}


 // CONSTRUCTOR
 public MyAccountListCntrlr(){
      acctList = new List<AccountWrapperCls>();
      selAccountNames = new Set<String>();
     showMessage = false;
      for(Account a : [SELECT AccountNumber, name, id, phone, fax, website, type, industry, description, NumberOfEmployees, BillingCity, AnnualRevenue from account
      LIMIT 10]){
           acctList.add(new AccountWrapperCls(a));
      }
 }

 // METHODS
 public void displaySelectedAccountNumbers(){
      //selAccountNames.clear();
      hasSelAcct = false;
      for(AccountWrapperCls cWrapper : acctList){
           if(cWrapper.isSelected){
                hasSelAcct = true;
                selAccountNames.add(cWrapper.cAccount.id);
                //showMessage = true;
                //message = 'The following error has ocurred: \n' + cWrapper.cAccount.id;
                //accts.add(cWrapper.cAccount);
           }
           if(selAccountNames.size() > 1)
            {
                for(Integer i = 1; i < selAccountNames.size(); i++)
                {
                    showMessage = true;
                    message = ' The selected accounts are: \n' + selAccountNames;
                } 
            }
      }
   }
I am also use a Wrapper Class such the following:
public class AccountWrapperCls {
 public Boolean isSelected {get;set;}
 public Account cAccount {get;set;}     

 public AccountWrapperCls(Account cAccount){
      this.cAccount = cAccount; } }//End AccountWrapperCls

And this is the VisualForce Page:
 
<apex:page controller="MyAccountListCntrlr" tabStyle="Account">

<!-- ACCOUNT LIST -->
     <apex:pageBlockTable value="{!acctList}" var="acctWrapper">
        <apex:column >
           <apex:inputCheckbox value="{!acctWrapper.isSelected}"/>
        </apex:column> 
        <apex:column value="{!acctWrapper.cAccount.AccountNumber}"/>
        <apex:column value="{!acctWrapper.cAccount.Name}"/>
         <apex:column value="{!acctWrapper.cAccount.website}"/>
     </apex:pageBlockTable>

      <!--//Message-->
         <apex:outputText rendered="{!showMessage}" id="tests">
             <script>
             alert("{!JSENCODE(message)}")
             </script>
         </apex:outputText>

     <!-- SELECTED ACCOUNT INFO -->
     <apex:pageBlockSection >
        <apex:outputPanel layout="block" rendered="{!hasSelAcct}">
           <apex:outputText value="Below are the selected account:"/>
           <br/>
           <apex:outputText value="{!selAccountNames}"/>
         </apex:outputPanel>
         <apex:outputPanel layout="block" rendered="{!NOT(hasSelAcct)}">
            <br/>
            <apex:outputText value="No account selected."/>
         </apex:outputPanel>
     </apex:pageBlockSection>
  </apex:pageBlock>

How can I merge the selected accounts? Any ideas or code? Thanks in advance.
 
Hello team, 
I am currently work on an app where I need to merge multiple ACCOUNT records at once, but I need to do it by selecting specific records. 
Does any one know how could I make this possible? 

for example, having a table with all accounts and by clicking specific records with a checkbox I will select what records I need to merge. 

Please, can somebody help me with this?
Thanks in advance.
Hello team I have the following: 
public void mergeOwners()
        { 
            String a1;
            String a2;
            
            a1 = sGetOwnerOne;
            a2 = sGetOwnerTwo;
            
            Account src = [SELECT Owner.Id FROM Account where Name like :a1];
            Account dest = [SELECT Id, Owner.Id FROM Account where Name like :a2];
            dest.OwnerId = src.OwnerId;
            update dest;
           
        }

These variables are taken from a two different selectLists
 
public static String sGetOwnerOne{set;get;}
    	public static String sGetOwnerTwo{set;get;}

But at clicking the button in visual force the system shows me the following error: 
List has no rows for assignment to SObject
Does somebody know why this happens?
Thanks in advance. 
 
Hello team, 

How can I convert all my leads to contacts without creating new opportunity?
Can somebody give me some example?

Thanks in advance. 
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?
 
I have the following code: 
Account master = new Account(Name='Account1')
insert master;

But this is for inserting a new account. How can I get an existing account with the same structure?
Something like: 
Account acc = account(Where name = :myVariable);
Hello team, 
I am currently work on an app where I need to merge multiple ACCOUNT records at once, but I need to do it by selecting specific records. 
Does any one know how could I make this possible? 

for example, having a table with all accounts and by clicking specific records with a checkbox I will select what records I need to merge. 

Please, can somebody help me with this?
Thanks in advance.
Hello team I have the following: 
public void mergeOwners()
        { 
            String a1;
            String a2;
            
            a1 = sGetOwnerOne;
            a2 = sGetOwnerTwo;
            
            Account src = [SELECT Owner.Id FROM Account where Name like :a1];
            Account dest = [SELECT Id, Owner.Id FROM Account where Name like :a2];
            dest.OwnerId = src.OwnerId;
            update dest;
           
        }

These variables are taken from a two different selectLists
 
public static String sGetOwnerOne{set;get;}
    	public static String sGetOwnerTwo{set;get;}

But at clicking the button in visual force the system shows me the following error: 
List has no rows for assignment to SObject
Does somebody know why this happens?
Thanks in advance. 
 
Hello team, 

How can I convert all my leads to contacts without creating new opportunity?
Can somebody give me some example?

Thanks in advance.