Sunday, October 28, 2012

Apex custom picklist creater from controller.

Code for custom select picklist:
public static List<selectOption> generateOptions( List<Schema.PicklistEntry> picklistEntry, 
                                                  Map<String, String> picklistMap, 
                                                  Boolean addNone, 
                                                  Boolean sortOptions ) {
    //Initializing list of options
    List<selectOption> options = new List<selectOption>();
    //Check if we need to add first empty option
    if( addNone )
        options.add( new selectOption( '', 'None' ) );
    //Check if we have picklist from sObject
    if( picklistEntry != null )
        for ( Schema.PicklistEntry items : picklistEntry )
            options.add( new selectOption( items.getLabel(), items.getValue() ) );
    // or from list
    if( picklistMap != null )    
        for ( String itemsId : picklistMap.keySet() )
            options.add( new selectOption( itemsId , picklistMap.get( itemsId ) ) );
    // check if we need to sort options
    if( sortOptions )
        options.sort();
    
    return options;
}
Enjoy.

No comments:

Post a Comment