You need to sign in to do that
Don't have an account?

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');
}
}
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');
}
}
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
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;
}
}
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>
Try this.
Please let me know if this help.
thanks
Shivdeep
It works for me ! Keep in touch :)