0

I have a multi-select picklist field 'PermissionsAllowed' at object 'AssignPermissions' with 4 values 'Country A User' , 'Country A Processor', 'Country B User'and 'Country B Processor'.

Now according to the user's country, he should see only relevant picklist value options and hide other two values.For Example, If the user is from Country A , he/she should see only 'Country A User' and 'Country A Processor' as values in multi-select picklist option.

How can we do this?

PS - here the focus is on 'how to hide selective values from multi select picklist ' thus you can ignore on filter part (based on country).

1

Using DescribeFieldResult you can get all the values and based on the logic you can filter the necessary values : -

public List<SelectOption> getCountryUser(){

    List<SelectOption> countryUser = new List<SelectOption>();
    Schema.DescribeFieldResult countryUserPkVal = Schema.ObjectName.FieldName.getDescribe();
    List<Schema.PicklistEntry> ple = countryUserPkVal.getPicklistValues();
    countryUser.add(new SelectOption('--None--', '--None--'));
    for(Schema.PicklistEntry f : ple){
        //Your Logic to check Logged in User Country and what you want show in the VF page      
        countryUser.add(new SelectOption(f.getLabel(), f.getValue()));
    } 
    return countryUser;           
}
  • Hi, Thanks for taking time but what I need is to Hide the values in Multiselect pick list. This code can be used if I have to create a custom multiselect pick list but what I desire to do is use same field but hide selective values – Rajeev Apr 12 '17 at 8:42
0

Answer - It doesn't seems possible to use the standard multi select pick list and hide values at UI.

Turnaround - You can create multi-select picklist using 2 selectLists one for Values Available and another for Selected Values to show only desired values from custom field at object ; Use two buttons in between those to select or deselect.

How do you do this ?

Following below Steps -

  1. take all the values from the picklist using DescribeFieldResult() and filter necessary values. See below code to do this.

    public List<SelectOption> getToSelectValues(){ List<SelectOption> countryUser = new List<SelectOption>(); Schema.DescribeFieldResult countryUserPkVal = Schema.ObjectName.FieldName.getDescribe(); List<Schema.PicklistEntry> ple = countryUserPkVal.getPicklistValues(); for(Schema.PicklistEntry f : ple){ //Your Logic to filter picklist values
    if(!rightvalues.contains(f.getLabel()) ) countryUser.add(new SelectOption(f.getLabel(), f.getValue())); } return countryUser;
    }

    1. For select button write below code

public PageReference selectclick(){ rightselected.clear(); for(String s : leftselected){ leftvalues.remove(s); rightvalues.add(s); } return null; }

  1. To get selected values use below function

    public List getSelectedValues(){ List options1 = new List(); List tempList = new List(); tempList.addAll(rightvalues); tempList.sort();
    for(String s : tempList) options1.add(new SelectOption(s,s)); return options1; }

  2. for deSelect/unselect use below code

    public PageReference unselectclick(){ leftselected.clear(); for(String s : rightselected){ rightvalues.remove(s); leftvalues.add(s); } return null; }

  3. Finally to the VisualForce page, paste below code

<apex:panelGrid columns="3" id="panelGridId"> <apex:selectList id="sel1" value="{!leftselected}" multiselect="true" style="width:223px" size="4"> <apex:selectOptions value="{!ToSelectValues}" id="firstOptions" /> </apex:selectList> <apex:panelGroup > <br/> <apex:image url="{!$Resource.selectImage}" height="20px" style="cursor:pointer;"> <apex:actionSupport event="onclick" action="{!selectclick}" reRender="panelGridId"/> </apex:image> <br/><br/> <apex:image url="{!$Resource.unSelectImage}" height="20px"> <apex:actionSupport event="onclick" action="{!unselectclick}" reRender="panelGridId"/> </apex:image> </apex:panelGroup> <apex:selectList id="sel2" value="{!rightselected}" multiselect="true" style="width:223px" size="4"> <apex:selectOptions value="{!SelectedValues}" /> </apex:selectList>

PS- do not forget to define the variables and initialize them before using

Public List<string> leftselected{get;set;}
Public List<string> rightselected{get;set;}
Set<string> leftvalues = new Set<string>();
Set<string> rightvalues = new Set<string>();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.