1

I have a dynamic picklist that I am displaying on the visualforce page.

Here is my visualforce code:

<apex:selectList value="{!SelectedNomination}" size="1" id="a">
   <apex:selectOptions value="{!contactlist}"></apex:selectOptions>
</apex:selectList>

Here is my apex code:

public Id SelectedNomination { get; set;}
public List<Nominator__c> Nominators = new List<Nominator__c>();

// The below code is in my class constructor method.

public List<SelectOption> contactlist
{

    get
    {

        String state = application.Congressional_State_Territory__c.substring(0,2);

        Nominators = [Select Id, Name, Class_Year__c, Contact_Name__c, 
              Nominator_Code__c, Nominator_Type__c from Nominator__c WHERE 
              FirstTwoLetters__c =: state and Nominator_Type__c= 
              'Congressional - House' and Class_Year__c =: 
              application.Class_Year__c and Status__c =:'Active' ];

        contactlist = new List<SelectOption>();

        for(Nominator__c nom : Nominators)
        {
            contactlist.add(new SelectOption(nom.Id, nom.Name + ' ' + 
            nom.Contact_Name__c));
        }

        return contactlist;
    }
    set {}
} 

I am saving the user chosen value in SelectedNomination variable and I am saving the value in congressional district field in the saveandcontinue fucntion

public pagereference saveAndContinue() {

    application.Congressional_District__c = SelectedNomination;     
    update application;

    Pagereference Page = new Pagereference('/apex/usafa_PCQ_Page_4');
    Page.setRedirect(true);
    return Page;

}

Test class:

I have started writing the code but it's in pieces and I am stuck.

Nominator__c Nominators = [Select Id, Name, Class_Year__c, Contact_Name__c, Nominator_Code__c, Nominator_Type__c from Nominator__c 
WHERE FirstTwoLetters__c =: 'GA' and Nominator_Type__c= 'Congressional - House' and Class_Year__c =: '2023' and Status__c =:'Active' ];

contactlist = new List<SelectOption>();

for(Nominator__c nom : Nominators)

{
    contactlist.add(new SelectOption(nom.Id, nom.Name + ' ' + 
    nom.Contact_Name__c));
}

//u is my current user

 system.runAs(u){

      usafa_PCQPage3Controller controllerClass = new usafa_PCQPage3Controller();

      List<SelectOption> selOpts=controllerClass.contactlist;

}
1
  • Not 100% sure but I think this line set {} maybe cause you some grief, a custom setter needs to at least look like set { contactlist = value; } or it won't actually set the value. A default setter just looks like set; without the {}. Commented Apr 13, 2018 at 13:59

1 Answer 1

2

Build your Nominator__c object with all the requirements that your query requires in the get;set;. You don't need to do that query in your Test class or build at that contactList.

List<Nominator__c> nomList = new List<Nominator__c>();

Nominator__c n = new Nominator__c();
n.Name = 'A Name';
//Plus whatever you need for your WHERE clauses
nomList.add(n);
n = new Nominator__c();
n.Name = 'B Name';
//Plus whatever you need for your WHERE clauses
nomList.add(n);
insert nomList;

Call your build method

usafa_PCQPage3Controller controllerClass = new usafa_PCQPage3Controller();
List<SelectOption> options = controllerClass.contactlist;

System.assertEquals(options.size(), 2);

You should also assert that you have the correct names in the picklist.. Just remember the only thing you need to test here is the actual method. nothing before, nothing after. Just the creation of the select options

7
  • Thanks for replying. I tried the above solution. It gives me error on this line List<SelectOption> options = controllerClass.contactlist(); Error: Compile Error: Method does not exist or incorrect signature: void contactlist()
    – Student
    Commented Apr 12, 2018 at 20:52
  • @Student I added a line, usafa_PCQPage3Controller controllerClass = new usafa_PCQPage3Controller();
    – EricSSH
    Commented Apr 12, 2018 at 21:17
  • Yes I have added that line too still getting the error.
    – Student
    Commented Apr 12, 2018 at 21:31
  • I don't know then, your posted code is leaving out some context..
    – EricSSH
    Commented Apr 12, 2018 at 22:04
  • contactList isn't a function, you don't need the (), you should be able to access it via List<SelectOption> options = controllerClass.contactlist; Commented Apr 13, 2018 at 14:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .