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
eBiz03eBiz03 

Help with Test Code

Hi All,

 

Looking for some help creating a test method for the code below. New to Apex and running in to errors installing the Force.com IDE so any help is much appreciated.

 

public with sharing class massupdatefield{

 

public List<Contact> contacts{get; set;}
public list<selectoption> options{Get; set;}
public string selected{get; set;}
String saveUrl = ApexPages.currentPage().getParameters().get('retURL');

public massupdatefield(apexpages.standardsetcontroller ssc){
    contacts = (list<contact>)ssc.getSelected();
    Schema.DescribeFieldResult FR = Contact.Rep_Call_List__c.getDescribe();
    List<Schema.PicklistEntry> PL = FR.getPicklistValues();
    options = new list<selectoption>();
    for(schema.PickListEntry p:PL){
       options.add(new selectoption(p.getLabel(), p.getvalue()));
    }
 
}

public pagereference updatecontacts(){

for(Contact c:contacts){
     c.Rep_Call_List__c = selected;
    }
   update contacts;

return new pagereference(saveurl);
}
}

Ankit AroraAnkit Arora

Hi,

 

This is the code which you need, hopefully it will cover the code coverage.

 

 

public static testMethod void testController()
{
	Contact con = new Contact() ;
	con.LastName = 'TestContact' ;
	insert con ;

	ApexPages.StandardController sc = new ApexPages.StandardController(con);

	//Replace /home.home.jsp with your desired reURL
	ApexPages.CurrentPage().getParameters().put('retURL' , /home.home.jsp) ;

	massupdatefield controller = new massupdatefield(sc) ;
	controller.updatecontacts() ;
}

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

 

eBiz03eBiz03

Getting a failure error:

 

Save error: Constructor not defined: [massupdatefield].<Constructor>(ApexPages.StandardController) massupdatefield.cls

Ankit AroraAnkit Arora

Try this :

 

 

public static testMethod void testController()
{
	Contact con = new Contact() ;
	con.LastName = 'TestContact' ;
	insert con ;
	
	List<Contact> conLst = new List<Contact>() ;
	conLst.add(con) ;

	ApexPages.StandardSetController sc = new ApexPages.StandardSetController(conLst);

	//Replace /home.home.jsp with your desired reURL
	ApexPages.CurrentPage().getParameters().put('retURL' , /home.home.jsp) ;

	massupdatefield controller = new massupdatefield(sc) ;
	controller.updatecontacts() ;
}

 

 

Hope this will work.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Ankit AroraAnkit Arora

Please mark it as a solution if it resolved your problem.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page