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
Carla CookCarla Cook 

how-to build selectList conditionally from a VF page using apex

Hi Team,

I've asked similar question, but answer did not solve issue.  Is this an order of execution thing or something like that???
See below,  where I tried by ultimate goal of condition based on RecordType.Name,  even tried  a simple IF (1=2)  and I still get ALL of the selectOption displayed.

What is the proper technique for this????
Thanks.

I am using this...
<apex:page standardController="MyCustom__c" sidebar="false">
.....
 <apex:form >
....
<apex:pageBlockSection title="Build PickList">
......
<apex:selectList >
    { ! IF (1=2)
    {
     <apex:selectOption itemLabel="1=1" itemValue="1=1"/>
    }
    }
.....
ultimate goal is this conditional,  but even the IF (1=2) does not seem to execute properly
I DO get the MyCustom__c.RecordType.Name A-OK.
However,  the IF seems to fail,  because I get ALL THREE selectOption displayed every time.

{ !IF 
           (CONTAINS (MyCustom__c.RecordType.Name,"Hiring"),
           {
           <apex:selectOption itemLabel="Initial Contact for Hiring Need" itemValue="Initial Contact"/>
           <apex:selectOption itemLabel="Follow-up Contact for Hiring Need" itemValue="Followup Contact"/>}
           , 
           
           {  <apex:selectOption itemLabel="NOT Hiring Need" itemValue="NOT Hiring Need"/>}
           )
        } 

  </apex:selectList>


 
Best Answer chosen by Carla Cook
Deepak Kumar ShyoranDeepak Kumar Shyoran
I don't this will be working as you're trying to write apex formate code on VF page, as this is a VF page and you have to write code by using VF page component with their proper formate.

For ex : As in your above problem you are trying to print apex:sectionOption conditionality with the help of If and contains condition operator instead of rendered attribute of <apex:selectOption>.
<apex:selectOption itemLabel="Initial Contact for Hiring Need" itemValue="Initial Contact" rendered="{!IF(MyCustom__c.RecordType.Name == 'Hiring',true,false)}" />
<apex:selectOption itemLabel="Follow-up Contact for Hiring Need" itemValue="Followup Contact" rendered="{!IF(MyCustom__c.RecordType.Name == 'Hiring',true,false)}" />
<apex:selectOption itemLabel="NOT Hiring Need" itemValue="NOT Hiring Need" rendered="{!IF(MyCustom__c.RecordType.Name == 'Hiring',false,true)}" />

Also you can bind a apex class variable of list<SelectionOptions> type which you can create dynamically from backend.

Hope this will help you.


 

All Answers

Deepak Kumar ShyoranDeepak Kumar Shyoran
I don't this will be working as you're trying to write apex formate code on VF page, as this is a VF page and you have to write code by using VF page component with their proper formate.

For ex : As in your above problem you are trying to print apex:sectionOption conditionality with the help of If and contains condition operator instead of rendered attribute of <apex:selectOption>.
<apex:selectOption itemLabel="Initial Contact for Hiring Need" itemValue="Initial Contact" rendered="{!IF(MyCustom__c.RecordType.Name == 'Hiring',true,false)}" />
<apex:selectOption itemLabel="Follow-up Contact for Hiring Need" itemValue="Followup Contact" rendered="{!IF(MyCustom__c.RecordType.Name == 'Hiring',true,false)}" />
<apex:selectOption itemLabel="NOT Hiring Need" itemValue="NOT Hiring Need" rendered="{!IF(MyCustom__c.RecordType.Name == 'Hiring',false,true)}" />

Also you can bind a apex class variable of list<SelectionOptions> type which you can create dynamically from backend.

Hope this will help you.


 
This was selected as the best answer
Carla CookCarla Cook
Thank you so very much.

Now I get it.   The conditional logic needs to be in the 'rendered' logic.   

thanks again.