[SalesForce] Dynamic picklist values in visualforce based on users profile

I have status field with picklist values draft,send to Rep,Return to Manager,Complete.I want to show only one picklist value (Return To Manager)for one profile (Rep profile) when user is on Send To Rep status.I don't want to show other values(Draft ,Complete) for User with Rep Profile .How to achieve this on visualforce

I know this can't be done without record types . Any other approach

Currently I am displaying picklist field in visualforce page with apex:inputField tag

Best Answer

Im assuming your controller property to get the options looks something like one of the following

If you are taking the values directly from a specific picklist field

public List<SelectOption> getMyOptions() {

        List<SelectOption> options = new List<SelectOption>(); 
        Schema.DescribeFieldResult field = yuopurObject.YourField.getDescribe();

        for (Schema.PicklistEntry f : field.getPicklistValues()){
            options.add(new SelectOption(f.getLabel(), f.getLabel())); 
        }     
        return options;     
}

Or if you are manually adding the options, then something like this

public List<SelectOption> getMyOptions() {

        List<SelectOption> options = new List<SelectOption>(); 
        options.add(new SelectOption('Option1','Option1'));
        options.add(new SelectOption('Option2','Option2'));
        options.add(new SelectOption('Option3','Option3'));
        return options;     
}

If you want to adjust the options according to profile then you could simply do something like this

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 From Profile Where Name = 'Your Profile Name'];
      if(System.Userinfo.getProfileId() = p.Id){
           //add your values here
      }else{
           //add alternate values here
      }

      return options;       
}
Related Topic