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
VaderVader 

Picklist Rendering Issues

I have a picklist that I am trying to generate from a class.  The idea is that when a user picks an item from the list that they will be redirected to the corresponding page.  When I use the code snippet below in a test page where the class is set as the "Controller=class" it works great.  However, when I add the class to another page as "extensions=class", the drop down list will not render any values.

 

Any ideas on how to get this to render properly.

 

Visualforce:

 

<apex:page standardController="Task" extensions="taskManagementRedirectPicklist" tabStyle="Task">
<apex:form >	
	<apex:pageBlock title="Selection Criteria">		
		
		<apex:pageBlockSection showHeader="false" title="Options" columns="3">
			<apex:pageBlockSectionItem >
				
				<apex:selectList value="{!picklistvalue}" size="1" >
			    	<Apex:selectOptions value="{!item}"/>
			    	<apex:actionSupport event="onchange" action="{!redirect}" />    
			    </apex:selectList>
			    		
			</apex:pageBlockSectionItem>
	    </apex:pageBlockSection>
	</apex:pageBlock>
</apex:form>
</apex:page>

 

Apex Class:

 

public with sharing class taskManagementRedirectPicklist {
	
	private ApexPages.StandardController controller;
	
	public taskManagementRedirectPicklist(ApexPages.StandardController stdController) {
      controller = stdController;
   }   
	
	public list<selectoption>item{get;set;}
	public string picklistvalue{get;set;}
	
	public taskManagementRedirectPicklist()
	{
	    item=new list<selectoption>();
	    item.add(new selectoption('myTasks','Tasks Assigned to Me'));
	    item.add(new selectoption('myDelegatedTasks','Tasks I Assigned to Others'));
	}
 
	public pagereference redirect()
	{
	     PageReference pageRef= new PageReference('/apex/'+picklistvalue);
	    pageRef.setredirect(true);
	    return pageRef;
	}

}

 

Best Answer chosen by Admin (Salesforce Developers) 
vbsvbs
Vader - As your code currently stands when you use the above custom controller as an extension the constructor with the ApexPages.StandardController parameter is executed whereas the picklist initialisation code is in the default constructor. To fix your issue you can:
1. Change the VF page constructor to this Apex class by using controller="taskManagementRedirectPicklist" and not as extension.
2. Call the default controller in the constructor public taskManagementRedirectPicklist(ApexPages.StandardController stdController) by adding the line "this();" This should call the default constructor code and set the picklist
3. Add the code for picklist initialization to the parametrized constructor as well (this is a bad way of doing it due to introducing redundant code)

All Answers

vbsvbs
Vader - As your code currently stands when you use the above custom controller as an extension the constructor with the ApexPages.StandardController parameter is executed whereas the picklist initialisation code is in the default constructor. To fix your issue you can:
1. Change the VF page constructor to this Apex class by using controller="taskManagementRedirectPicklist" and not as extension.
2. Call the default controller in the constructor public taskManagementRedirectPicklist(ApexPages.StandardController stdController) by adding the line "this();" This should call the default constructor code and set the picklist
3. Add the code for picklist initialization to the parametrized constructor as well (this is a bad way of doing it due to introducing redundant code)
This was selected as the best answer
VaderVader

I really appreciate the feedback!

 

Needed to go with option 2 above since I am using components from the Task object in other sections of the VF page.  

 

There are so many holes in what I know regarding Apex and VF pages but as you stated above, the "this();" code line added to the constructor got it working.  Just wish I understood fully why!  :-)  I never would have gotten that on my own.  Thanks!

 

Corrected code below:

 

public with sharing class taskManagementRedirectPicklist {
	
	private ApexPages.StandardController controller;
	
	public taskManagementRedirectPicklist(ApexPages.StandardController stdController) {
		this();
      controller = stdController;
   }   
	
	public list<selectoption>item{get;set;}
	public string picklistvalue{get;set;}
	
	public taskManagementRedirectPicklist()
	{
	    item=new list<selectoption>();
	    item.add(new selectoption('page1','List Option 1'));
	    item.add(new selectoption('page2','List Option 2'));
	}
 
	public pagereference redirect()
	{
	     PageReference pageRef= new PageReference('/apex/'+picklistvalue);
	    pageRef.setredirect(true);
	    return pageRef;
	}

}