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

Show picklist value on visualforce page for selected users

Hey guys,

We have an apex class and VF page called Register.  On this page the user can select from 4 different values on a picklist field called Type.

Is it possible to show all fours selections to all users that have Agent (checkbox) as TRUE on their user profile?

So what I would need is any currently logged in user with Agent as TRUE will see Options 1,2,3,4 and any user with Agent as FALSE will see options 1,2,3.

Can this be done?

Dhriti Moulick 16Dhriti Moulick 16
Can you please illaborate, these 4 different picklist values you are displaying in VF page is stored in any objects?
Hi Rad,

Please find below code, that will help you in fulfilling your requirement:

Add valued based on Profile and Agent check box:
public List<SelectOption> getMyOptions() {

      List<SelectOption> options = new List<SelectOption>(); 

      //Might want to look into storing in a custom setting to avoid SOQL call
      Profile p = [Select Id, Name, agent__C From Profile Where Name = 'Your Profile Name'];
      if(p != null && p.agent__c == true ){
           //add your values here
           //add alternate values here

      return options;       

Hope this will helps you.


Skype: gaurav62990

You need to create your own SelectOption in apex class. this cannot be done using a standard Picklist field rather you can do a validation on code level. 

Refer this code : 

<apex:page controller="selectOptionBasedonUser">
<apex:form >
<apex:pageBlock >    
    <apex:outputLabel > <b> Testing : </b> </apex:outputLabel>
    <apex:selectList size="1" value="{!selectedTest}">
      <apex:selectOptions value="{!TestLst }"/>
    </apex:selectList> <br/>
    </apex:pageBlock >  

public class selectOptionBasedonUser {
    public boolean istrue;
    public List<SelectOption> TestLst {get;set;}
    public String selectedTest{get;set;}
    public selectOptionBasedonUser (){
        for(User iUs : [Select id, name, Agent__c FROM USER WHERE id =: userinfo.getuserid()]){
            istrue = iUs.Agent__c;
        TestLst = new List<SelectOption>();
        TestLst.add(new SelectOption('','--None--'));
            TestLst.add(new SelectOption('Test1','Test1',false));
            TestLst.add(new SelectOption('Test2','Test2',false));
        TestLst.add(new SelectOption('Test3','Test3',istrue));


Dhriti Moulick 16Dhriti Moulick 16

  You can go through with the following approach,

  Create two fields in User object:


   2.Type(Picklist) -put values example-(1,2,3,4)

Now to do the following configuration.

1.Make agent (checkbox) as controlling field 
2.Make Type(Picklist) as dependent Field.

Go to fields,click on Field Dependency button,Create a new Dependency,

a.Based on checked value select picklist values.
b.Based on unchecked value select picklist values.

Configuration part is over.

Now execute the following code:

   public User usr {get;set;}
   User usr=[Select id, name, Agent__c,Type FROM USER WHERE id =: userinfo.getuserid() LIMIT 1];

and display the above info in VF page.

If you have any concern please let me know.