Skip to main content

Search for a picklist value in an org

We have a scenario where we need to search for any picklist which has a value 'Banking' in an organization.


To find out this value, We are going to use Schema Class and Schema Namespace which has classes and methods to get the list of objects, fields and picklist values.

  1. Schema Class (from System Namespace) contains methods for obtaining schema describe information.
  2. Schema Namespace provides classes and methods for schema metadata information.

To read more,  check Schema Class  and Schema Namespace 


Below is the sample code to search for a picklist value 'Banking' across the org. 

List < Schema.SObjectType > gd = Schema.getGlobalDescribe().Values();      

 for ( Schema.SObjectType f : gd ) {  

    Schema.DescribeSObjectResult dsr = f.getDescribe();   

    Map<String , Schema.SObjectField > mapFieldList = dsr.fields.getMap();    

    for ( Schema.SObjectField field : mapFieldList.values() ) {    

        Schema.DescribeFieldResult fieldResult = field.getDescribe();    

        if ( fieldResult.getType()== Schema.DisplayType.Picklist ) {     

           List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

   for( Schema.PicklistEntry pickListVal : ple){

   if(pickListVal.getLabel()!= null && pickListVal.getLabel().contains('Banking'))

           {

   System.debug('Object: '+dsr.getLabel()+'picklist: ' +fieldResult.getLabel()+', Value : '+pickListVal.getLabel());   }

}      

        }   

    }  

}


Thank you for your time in reading this. Cheers :)

Comments