Thursday, October 4, 2012

SalesForce: Searching with Multipicklist Available Values

         I have to crate a custom search form which has a drop-down having a multi-pick list available values as options. The search is to find some child records based on any one of the selected value of a multi-pick list . So I have crated a pick list (drop-down) with options as the available values of the multi-pick list . For that I find a solution to get available values of a multi-pick list by Apex coding.


Retrieving  available values of a multi-pick list

   The used function as below:

  public List<selectOption> getPickListValues(Sobject objectName, String fieldName, String firstValueLabel, String firstValue) {

            List<selectOption
> options = new List<selectOption>();
            if (firstValueLabel != null) { //if first value
                options.add(new selectOption(firstValue, firstValueLabel)); //add the first option
            }
            Schema.sObjectType sobjectType = objectName.getSObjectType(); //get the sobject from schema
            Schema.DescribeSObjectResult sobjectDescribe = sobjectType.getDescribe(); //get the describe of sobject
            Map fieldMap = sobjectDescribe.fields.getMap(); //get a map of all  fields for sobject
            List pickListValues = fieldMap.get(fieldName).getDescribe().getPickListValues(); //get the list of picklist values for the field on the sobject
            for (Schema.PicklistEntry a : pickListValues) { //for all values in the picklist list
                if(a.getValue() != 'unknown' && a.getValue() != 'Other' )        
                    options.add(new selectOption(a.getValue(), a.getLabel())); //add the value and label to options
            }
            return options;
        }



  For eg:  getPickValues(new Account(), 'Select_List__c', 'All', 'All');

   Here my parent object is Account and the field is Select_List__c.

Using multi-pick list value in SOQL

   The query that I used as:

    [SELECT name FROM child__c WHERE Account__r.Select_List__c INCLUDES (:filter)]; 



References:

http://boards.developerforce.com/t5/Apex-Code-Development/How-to-retrieve-multi-select-picklists-using-includes-excludes/td-p/172210

http://boards.developerforce.com/t5/Apex-Code-Development/Looping-through-multi-select-picklist-values/td-p/401647 

NB: This is for my future reference only

3 comments:

  1. Hi,
    I displayed all fields dynamically in an visualforce page without using fieldset.In my visualforce page some multiselectpicklist fields are there.Every time i will create multiselect fields and those fields add to my visualforce page Dynamically.How to display created multiselectpicklist field values as checkboxes Dynamically in salesforce?

    help me........

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi mahesh,

    Please check this

    http://salesforce.stackexchange.com/questions/18984/displaying-multiselect-picklist-as-checkboxes

    ReplyDelete