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
sachitanand kumarsachitanand kumar 

Hi, I m new at salesforce. Anybody help me to write Test class for this apex class.

public class AccountOwnerUpdate{
    public Boolean isSelected{get;set;}
    public Account accounts{get;set;}
    public List<Account> selectedAccounts{get;set;}
   // public Boolean sendEmail{get;set;}
    public AccountOwnerUpdate(ApexPages.StandardSetController standardController)
    {   
        if(standardController.getSelected().size() == 0){
            isSelected = false;
            ApexPages.Message msg = new ApexPages.message(ApexPages.Severity.ERROR,'No Account Selected');
            ApexPages.addMessage(msg);
        }else{
            isSelected = true;
            selectedAccounts=  [SELECT id,name,ownerid from Account where id=:standardController.getSelected()];
           accounts = selectedAccounts[0]; 
           // accounts.ownerid = null; 
       }       
    }
    public PageReference updateAccount()
    {   
        for(Account a: selectedAccounts){
            a.ownerid = accounts.ownerid;
        }
        update selectedAccounts;
        return new Pagereference('/001/o');
    }

}
Best Answer chosen by sachitanand kumar
Shivdeep KumarShivdeep Kumar
Hi Kumar,

Please try below code !

@isTest
public class AccountOwnerUpdate_test {
    
    static testMethod void data1(){
        List<Account> accounts = new List<Account>();
        Account a = New Account();
        a.Name = 'myAccount';
        accounts.add(a);
        insert accounts;
         
    
        ApexPages.StandardSetController  sc = new ApexPages.StandardSetController (accounts);
        sc.setSelected([SELECT Id, OwnerId FROM Account LIMIT 2]);
        AccountOwnerUpdate aop = new AccountOwnerUpdate(sc);
        aop.isSelected = true;
      
     aop.updateAccount();
             
    }
    
    static testMethod void data2(){
        List<Account> accounts = new List<Account>();
        Account a = New Account();
        a.Name = 'myAccount';
        accounts.add(a);
        insert accounts;
         
    
        ApexPages.StandardSetController  sc = new ApexPages.StandardSetController (accounts);
        AccountOwnerUpdate aop = new AccountOwnerUpdate(sc);
        aop.isSelected = false;
    }
}

All Answers

Shivdeep KumarShivdeep Kumar
Hi Kumar,

Please try below code !

@isTest
public class AccountOwnerUpdate_test {
    
    static testMethod void data1(){
        List<Account> accounts = new List<Account>();
        Account a = New Account();
        a.Name = 'myAccount';
        accounts.add(a);
        insert accounts;
         
    
        ApexPages.StandardSetController  sc = new ApexPages.StandardSetController (accounts);
        sc.setSelected([SELECT Id, OwnerId FROM Account LIMIT 2]);
        AccountOwnerUpdate aop = new AccountOwnerUpdate(sc);
        aop.isSelected = true;
      
     aop.updateAccount();
             
    }
    
    static testMethod void data2(){
        List<Account> accounts = new List<Account>();
        Account a = New Account();
        a.Name = 'myAccount';
        accounts.add(a);
        insert accounts;
         
    
        ApexPages.StandardSetController  sc = new ApexPages.StandardSetController (accounts);
        AccountOwnerUpdate aop = new AccountOwnerUpdate(sc);
        aop.isSelected = false;
    }
}
This was selected as the best answer
Stancioiu Stefan 1Stancioiu Stefan 1
Hi Experts,

I 'm new to coding and need help to write test class for this method.It takes the parameter from the visualforce page and if a contact is set as primary_contact, when you click on other contact, make that contact primary and deselect the one before.

  public void processLinkClick() {
        Id id1 = System.currentPageReference().getParameters().get('param');
        //For loop to iterate through all the Contact records
        for(Contact c : contactList) {
            if(c.Id == id1) {
                //check the contact and set as primary
                c.Is_Primary_Contact__c = true;
            } else {
                //uncheck contact from being primary
                c.Is_Primary_Contact__c = false;
            }
        } //end for
    //Update DML statement for the Contact List
        try {
            update contactList;
            System.debug('Contact updated with succes');
        } catch(Exception e) {
            System.debug('Contact not updated');
        }
    
    }


<apex:commandLink rendered="{!!ctt.Is_Primary_Contact__c}" value="Change" onclick="confirmation()" action="{!processLinkClick}"
           <apex:param name="param" value="{!ctt.id}" assignTo="{!AccId}"/>
 </apex:commandLink>
Shivdeep KumarShivdeep Kumar
Hi Stefan ,

Try this.
 
public static testMethod void data1(){

Contact c = New Contact();
c.Lastname = 'my LastName';
insert c;

System.CurrentPageReference().getParameters().put('param',c.Id);


}

public static testMethod void data2(){

Contact c = New Contact();
c.Lastname = 'my LastName';
insert c;


System.CurrentPageReference().getParameters().put('param','03824082323423');


}

Please let me know if this help.

thanks
Shivdeep
Stancioiu Stefan 1Stancioiu Stefan 1
Thanks Shivdeep Kumar.
It works for me ! Keep in touch :)