2

I use the following code in order to retrieve the list of configured states in the org.

private static List<String> getStateList()
{
    List<String> stateList = new List<String>();
    Schema.DescribeFieldResult fieldResult = User.statecode.getDescribe();
    List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();        
    for( Schema.PicklistEntry f : ple){
        stateList.add(f.getLabel());
    }
    
    
    return stateList;
}

However, when trying to deploy the code, I run into the following error: Variable does not exist: User.statecode

Can anyone shed some light on this issue - I am unable to deploy without this error being fixed.

2
  • 4
    Is State Country Picklist enabled? Commented Jun 17, 2021 at 15:19
  • 1
    Welcome to Salesforce Stack Exchange. Thank you for including the actual error. But please edit your question to also include where you are attempting to deploy the code (e.g., scratch org, sandbox, Production). It could make a difference to the specifics of an answer. Commented Jun 17, 2021 at 15:24

3 Answers 3

5

StateCode field is available for org if country picklist is enabled in the org.

Make sure, that you have it enabled for your environment.

in lightning:

Setup -> Data -> State and Country/Territory Picklists -> Complete all the steps

2
  • Thanks for the quick reply! I had thought that I already turned it on in the target org but this has solved my issue :) Commented Jun 18, 2021 at 7:59
  • But why...? Why do I need to complete all those steps just to be able to reference it in the Apex? Those fields are actively visible, available, usable, referenceable, and--for my intended purposes--fully functional in the UI as-is. Blegh. Commented Nov 21 at 20:28
3

If you're trying to build an Unlocked Package, you need to specify this feature in your Scratch Org Definition File.

Example definition file:

{ "edition": "Enterprise", 
  "features": ["StateAndCountryPicklist"]
}
1
  • Thank you! I wasn't but this is good to know for the future :) Commented Jun 18, 2021 at 8:00
2

As per the documentation you need to first:

Makes sure the state and country picklist is enabled in the ORG

This is likely the problem.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.