Saturday, March 23, 2013

How to get label and value from picklist field in Apex?

How to get label and value from picklist field?
For example I have custom object Candidate__c with field Gender which is Picklist datatype.I want to read the value from the Apex code. So here the way, we can retrieve the value or label of the picklist
with the code below

 Schema.DescribeFieldResult fieldResult = Candidate__c.Gender__c.getDescribe();  
 List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();  
  for (Schema.PicklistEntry f : ple) {  
  System.debug('Label ='+ f.getLabel()+' === Value ='+ f.getValue());  

Okay, that's how it's look. Here the output.

 USER_DEBUG|[4]|DEBUG|Label =Male === Value =Male  
 USER_DEBUG|[4]|DEBUG|Label =Female === Value =Female  

Have a nice day! :)