MK Partners

Dynamic Picklist Creation and Sorting

Here’s a way to dynamically create a Picklist in Apex, pre-sort the list, and present it in a Visualforce page. In Salesforce it’s not possible to dynamically change Picklist values without getting deep into Force.com API programming. If you wanted to prepare a list of all Contact field names and their labels including custom fields with current information, you would need to generate this dynamically with metadata. To accomplish this we can generate it in Apex and present it in a way that looks like a native Salesforce Picklist.

I’ve included usable code for grabbing Contact metadata so we can present the field Labels in the list and return the Value (Name, AccountId, OwnerId, CustomField1__c, CustomField2__c, etc.) The goal is to present the field labels in alphabetical order with “–None–” listed at the top. When selected this list will return the Id or actual name of the field as opposed to the presented field label. With these results you can use DML to get or set values in Contact.

Visualforce element:

<apex:pageBlockSectionItem >
   <apex:outputLabel value="Contact:" for="contactlist"/>
   <apex:selectList id="contactlist" value="{!contactField}" size="1">
      <apex:selectOptions value="{!contactFieldOptions}"/>
   </apex:selectList>
</apex:pageBlockSectionItem>

Value=”{!contactField}” is the public property defined in the Apex controller class:

public String contactField {get; set;}

//Function to load and sort Contact field metadata for binding to: “{!contactFieldOptions}”
public void loadContactFields()
{
              Map<String, Schema.SObjectField> m = Schema.SObjectType.Contact.fields.getMap();
              List<Schema.SObjectField> sof = m.values();
              contactFieldOptions = new List<selectOption>();
              Map<String, String> tmp = new Map<String, String>();
              List<String> tmp2 = new List<String>();

              for(Schema.SObjectField s : sof)
              {
                            tmp.put(s.getDescribe().getLabel(), s.getDescribe().getName()); // Reversed for sorting
                            tmp2.add(s.getDescribe().getLabel());
              }

              tmp2.sort(); // Sort by field label, not field name
              selectOption so = new selectOption('NONE', '--None--'); // Native SF look and feel
              contactFieldOptions.add(so); // Build the Select List

              for(String s : tmp2)
              {
                            so = new selectOption(tmp.get(s), s);
                            contactFieldOptions.add(so);
              }
}

Finally, you can use the result(s) for dynamic Apex operations such as SOQL:
s = Database.query(contactField + ‘, Namespace__CustomField1__c from Contact limit 1000′);

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>