+ Start a Discussion
SFDC-NOOBSFDC-NOOB 

Wrapper Class Code Coverage

Hello,

 

I am trying to get coverage on my controller.  I am at 55% but I can't seem to access the wrapper class.  Please advise.  Lines in red are not covered.  My code is as follows:

Thank you in advance.

 

 

*****************************************************************************CONTROLLER*****************************************************************

public class UpForGrabs{

date d = date.today();
date LastTdate = date.today().addDays(-90);
date taskduedate = date.today().adddays(7);
date OppClosedate = date.today().addDays(14);

public List<aAccount> accountList{get; set;}
public List<opportunity> oppsList{get;set;}
public List<contact> contactList {get;set;}
public boolean limitcheckbox{get;set;}



public List<aAccount> getAccounts() {

if(accountList == null) {

accountList = new List<aAccount>();

for(account a: [select id, Name, type, ownerid, lastmodifieddate FROM ACCOUNT WHERE lastactivitydate <:LastTDate AND type = 'Active Opportunity' AND LastModifiedDate <:d ORDER BY LastModifiedDate Desc limit 1000]) {

 

accountlist.add(new aAccount(a));

}
}
return accountList;
}

public list<opportunity> getOpps(){
oppsList = [SELECT AccountId, OwnerId FROM Opportunity WHERE Stagename != 'Closed Lost' AND Stagename != 'Closed Won'];
return oppsList;
}

public list<contact> getcontacts(){
contactList = [Select accountid, ownerid FROM Contact];
return contactList;
}

public PageReference processSelected() {

List<account> selectedaccounts = new List<account>();

 

for(aAccount aAcct: getAccounts()) {
if(aAcct.selected == true) {
selectedAccounts.add(aAcct.acct);
}
}

 

System.debug('These are the selected accounts...');

for(account acct: selectedaccounts) {
acct.Ownerid = UserInfo.getUserId();
system.debug(acct);
update acct;

for(opportunity o: getOpps()){
if(o.accountid == acct.id){
o.ownerid = userinfo.getuserid();
update o;

system.debug(o);
}
}

for(contact c: getContacts()){
if(c.accountid == acct.id){
c.ownerid = userinfo.getuserid();
system.debug(c);
update c;
}
}

task t = new task(ownerid = userinfo.getuserid(),whatid=acct.id, subject = 'Call', Priority = 'Normal', Status= 'Not Started',activitydate = taskduedate );
insert t;
}
accountList = null;
return null;
}

 

//THIS IS MY WRAPPER CLASS


public class aAccount {
public account acct {get; set;}
public Boolean selected {get; set;}

 

public aAccount (Account a) {

acct = a;
selected = false;
}

}
}

 

 

**********************************************************************************TEST CLASS*********************************************************************************

 

@istest
public class Testupforgrabs{
public static TestMethod Void Testupforgrabs(){
    date mydate = date.today();
    
    pagereference pageRef = page.upforgrabs;
    Test.setCurrentPage(pageRef);

        upforgrabs UFG = new upforgrabs();
        
        account a = new account( Name='Test',phone='4109011267',macro_industry__c='Construction',custom_account_source__c='Referral',preferred_mode_s__c='Van');
        insert a;
                
        a.ownerid = userinfo.getuserid();
        update a;
        
        contact c = new contact(accountid=a.id,firstname='John',lastname='Doe');
        insert c;
        
        if(c.accountid == a.id){
        c.ownerid = userinfo.getuserid();
        }
        update c;
        
        task t = new task(ownerid=userinfo.getuserid(), whoid=c.id,subject='Email',whatid=a.id,priority='Normal',Status='Not started');
        insert t;
        
        opportunity opp = new opportunity(ownerid=userinfo.getuserid(), accountid=a.id, name = 'TestOpp',stagename='Discovered',closedate=mydate);
        insert opp;
        
        UFG.getaccounts();
        UFG.getcontacts();
        UFG.getOpps();
        UFG.processselected();
}
}

hitesh90hitesh90

Hi Josh,

 

You have to update your Test code as below.

 

Test Class:

@istest
public class Testupforgrabs{
public static TestMethod Void Testupforgrabs(){
    date mydate = date.today();    
    pagereference pageRef = page.upforgrabs;
    Test.setCurrentPage(pageRef);

        upforgrabs UFG = new upforgrabs();
        
        account a = new account( Name='Test',phone='4109011267',type = 'Active Opportunity',lastactivitydate = sydate.today().addDays(-100), macro_industry__c='Construction',custom_account_source__c='Referral',preferred_mode_s__c='Van');
        insert a;
                
        a.ownerid = userinfo.getuserid();
        update a;
        
        contact c = new contact(accountid=a.id,firstname='John',lastname='Doe');
        insert c;
        
        if(c.accountid == a.id){
        c.ownerid = userinfo.getuserid();
        }
        update c;
        
        task t = new task(ownerid=userinfo.getuserid(), whoid=c.id,subject='Email',whatid=a.id,priority='Normal',Status='Not started');
        insert t;
        
        opportunity opp = new opportunity(ownerid=userinfo.getuserid(), accountid=a.id, name = 'TestOpp',stagename='Discovered',closedate=mydate);
        insert opp;
        
	UFG.d = date.today().addDays(2);
        UFG.getaccounts();
        UFG.getcontacts();
        UFG.getOpps();
        UFG.processselected();
	}
}

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

SFDC-NOOBSFDC-NOOB

Thank you for your reply.

 

I updated my code to the code your provided; however I am getting a few errors.

 

1.  LastActivityDate is not a writeable field.

2.  UFG.d Variable is not visible.

 

Please advise and thank you.

hitesh90hitesh90

You have to make two changes.

 

1. Take below two variable as public in your class

     date d = date.today();
     date LastTdate = date.today().addDays(-90);

 

    change it to
    public date d = date.today();
    public date LastTdate = date.today().addDays(-90);

 

2. Try to use following test class.

 

@istest
public class Testupforgrabs{
public static TestMethod Void Testupforgrabs(){
    date mydate = date.today();    
    pagereference pageRef = page.upforgrabs;
    Test.setCurrentPage(pageRef);

        upforgrabs UFG = new upforgrabs();
        
        account a = new account( Name='Test',phone='4109011267',type = 'Active Opportunity', macro_industry__c='Construction',custom_account_source__c='Referral',preferred_mode_s__c='Van');
        insert a;
                
        a.ownerid = userinfo.getuserid();
        update a;
        
        contact c = new contact(accountid=a.id,firstname='John',lastname='Doe');
        insert c;
        
        if(c.accountid == a.id){
        c.ownerid = userinfo.getuserid();
        }
        update c;
        
        task t = new task(ownerid=userinfo.getuserid(), whoid=c.id,subject='Email',whatid=a.id,priority='Normal',Status='Not started');
        insert t;
        
        opportunity opp = new opportunity(ownerid=userinfo.getuserid(), accountid=a.id, name = 'TestOpp',stagename='Discovered',closedate=mydate);
        insert opp;
        
		UFG.d = date.today().addDays(2);
		UFG.LastTdate = date.today().addDays(2);
        UFG.getaccounts();
        UFG.getcontacts();
        UFG.getOpps();
        UFG.processselected();
	}
}

 

SFDC-NOOBSFDC-NOOB

Hitesh,

 

I made the changes and the errors are gone.  Unfortunately after running the test, I did not gain any more code coverage.  I am at 55%.  Any other ideas on how I can increase coverage?

 

Thanks you.