1

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? – David Cheng Jun 17 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. – Moonpie Jun 17 at 15:24
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

1
  • Thanks for the quick reply! I had thought that I already turned it on in the target org but this has solved my issue :) – Matthew Rosete Jun 18 at 7:59
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 :) – Matthew Rosete Jun 18 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.