Get Picklist Values Using Dynamic Apex

 

The picklist field name and object are the two parameters that the code snippet below requires. We can read either the Picklist label or the value based on these two inputs.


String objectName = 'Account';
String fieldName ='Industry';
  
Schema.SObjectType s = Schema.getGlobalDescribe().get(objectName) ;
Schema.DescribeSObjectResult r = s.getDescribe() ;
Map<String,Schema.SObjectField> fields = r.fields.getMap() ;
Schema.DescribeFieldResult fieldResult = fields.get(fieldName).getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for( Schema.PicklistEntry pickListVal : ple){
    System.debug(pickListVal.getLabel() +' ---- '+pickListVal.getValue());
} 



 

Comments

Popular posts from this blog

Agentforce in Production: What Separates a Successful Deployment from an AI Demo

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Salesforce Data Graphs - Best Practices, Limits & Guardrails — 2026 Edition