+ Start a Discussion
SayasoniSayasoni 

Redirect to different visualforce pages based on pick list value selected

Hi people,

I have a drop down on my visualforce page with two picklist values(Add to Contact & Add to Associate).I would like each picklist value to redirect to a diffrent visualforce page based on the value selected. i.e If i select 'Add to Contact', it should take me to a visualforce page(/apex/AddToContact) and if i select 'Add to Associate', it should take me to a different visualforce page(/apex/AddToAssociate).

 

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference:

 

------------- vf page--------------

<apex:page controller="redirectonthebasisonpicklist" >

 <Apex:form >

    

     <apex:selectList value="{!picklistvalue}" size="1" >

     <Apex:selectOptions value="{!item}"/>

    <apex:actionSupport event="onchange" action="{!redirect}" />    

     </apex:selectList>

 

 

 </Apex:form>

</apex:page>

 

--------------- apex controller --------------

 

public class redirectonthebasisonpicklist

 {

public list<selectoption>item{get;set;}

public string picklistvalue{get;set;}

public redirectonthebasisonpicklist()

{

    item=new list<selectoption>();

    item.add(new selectoption('addtocontact','add to contact'));

    item.add(new selectoption('AddtoAssociate','Add to Associate'));

}

 

public pagereference redirect()

{

     PageReference pageRef= new PageReference('/apex/'+picklistvalue);

    pageRef.setredirect(true);

    return pageRef;

}

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference:

 

------------- vf page--------------

<apex:page controller="redirectonthebasisonpicklist" >

 <Apex:form >

    

     <apex:selectList value="{!picklistvalue}" size="1" >

     <Apex:selectOptions value="{!item}"/>

    <apex:actionSupport event="onchange" action="{!redirect}" />    

     </apex:selectList>

 

 

 </Apex:form>

</apex:page>

 

--------------- apex controller --------------

 

public class redirectonthebasisonpicklist

 {

public list<selectoption>item{get;set;}

public string picklistvalue{get;set;}

public redirectonthebasisonpicklist()

{

    item=new list<selectoption>();

    item.add(new selectoption('addtocontact','add to contact'));

    item.add(new selectoption('AddtoAssociate','Add to Associate'));

}

 

public pagereference redirect()

{

     PageReference pageRef= new PageReference('/apex/'+picklistvalue);

    pageRef.setredirect(true);

    return pageRef;

}

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

This was selected as the best answer
SayasoniSayasoni

Thanks alot,this works ok,however when i select the second option('Add to Associate'),it loads the page for the first option('AddToContact). I guess this is because the page is being loaded with the default option set to 'Add to Contact'.

How do i set a different default option maybe 'Select an option',so that the remaining two options can work correctly based on their selected pages.

SayasoniSayasoni

Hi Ankit,

Any help on my question above? Am stuck.