function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tomeka WrayTomeka Wray 

Help in Retrieving Picklist Values from State field on Lead Object

I am trying to grab the picklist values from the State field on my Lead object. The State field is part of the Address date type. The following code returns nothing:
List<Schema.PicklistEntry>  fieldResult = Lead.State.getDescribe().getPicklistValues();
        system.debug('States = ' + fieldResult);
        
        Map<string,string> options = new Map<string,string>();
		for( Schema.PicklistEntry f : fieldResult )
   			{
      			options.put(f.getLabel(),f.getLabel());
                system.debug('State =' + f.getLabel());
   				}

 
Best Answer chosen by Tomeka Wray
RaidanRaidan
Hi Tomeka,

If you are using the State and Country picklists, you can retrieve the picklist values from the StateCode field.
List<SelectOption> options = new List<SelectOption>();
for (Schema.PicklistEntry pe : SObjectType.Lead.fields.StateCode.getPicklistValues()) {
    options.put(pe.getValue(), pe.getLabel()); 
}


 

All Answers

Anil kumar GorantalaAnil kumar Gorantala
Lead Edit Page
you see here state field is not pick list
I think you want status field values. if so, you can use this code
Schema.DescribeFieldResult fieldResult = lead.Status.getDescribe();
List<Schema.PicklistEntry> picklistValues = fieldResult.getPicklistValues();
for(Schema.PicklistEntry value : picklistValues)
{
    system.debug(value.getLabel());
}

you can use it for any Picklist
good luck
RaidanRaidan
Hi Tomeka,

If you are using the State and Country picklists, you can retrieve the picklist values from the StateCode field.
List<SelectOption> options = new List<SelectOption>();
for (Schema.PicklistEntry pe : SObjectType.Lead.fields.StateCode.getPicklistValues()) {
    options.put(pe.getValue(), pe.getLabel()); 
}


 
This was selected as the best answer
Anil kumar GorantalaAnil kumar Gorantala
Hi Raidan did you check the code you posted?
I get error msg "statecode is not a field of Lead".
RaidanRaidan
Hi Anil, 

You will have to enable State and Country picklist to be able to use the field.
Tomeka WrayTomeka Wray
Raidan,

Do you know how I can just retrive the states from the United states? Statecode returns all states for each country. Thanks. 
RaidanRaidan
I think you will have to filter the list yourself in the code. For example:
List<SelectOption> options = new List<SelectOption>();
for (Schema.PicklistEntry pe : SObjectType.Lead.fields.StateCode.getPicklistValues()) {
    if (pe.getValue() == 'US') {
        options.put(pe.getValue(), pe.getLabel());
    }
}