Get the Picklist Label value instead of API value in Apex #inSalesforce

Get the Pick list Label instead of API in Apex


Generally, we get API Name but if we want to get the Label Name of the picklist value


In Class:

Contact cont = [SELECT Id,Name,toLabel(Status__c) FROM Contact WHERE Id = '0035g00000TyeMlAAJ'];
System.debug('thePickListLabel::::::::::::::'+ cont.Status__c);



In Trigger:

Contact cont = [SELECT Id,Name,Status__c FROM Contact WHERE Id = '0035g00000TyeMlAAJ'];
//Get the pick list entries
List<Schema.PicklistEntry> values = Contact.Status__c.getDescribe().getPicklistValues();
Map<String,String> statusApiToLabelMap = new Map<String,String>();
For(Schema.PicklistEntry pe : values){
//Map to hold Pick list API as Key and Pick list Label as Value
statusApiToLabelMap.put(pe.getValue(), pe.values.getLabel());
}
System.debug('theLabel::::::::::'+ statusApiToLabelMap.get(cont.Status__c));

Comments