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
Abhilash Mishra 13Abhilash Mishra 13 

Create Country State Dependent Picklist In apex code

Hello, I need to create a country state dependent picklist in my visualforce page.
I want to Default State and Country/Territory Picklists. I am able to get values of both country and state as suggested here.
But they Are not dependent Enough.
How to make them Dependent.? or should i my own object.
 
Best Answer chosen by Abhilash Mishra 13
Abhilash Mishra 13Abhilash Mishra 13
Hi finally i was able to it by using , user fields. implmenting user.countrycode and user.statecode
how ever you have to use additonal JS as the value stored will be name of countries and states. not the codes. 

All Answers

Abhilash Mishra 13Abhilash Mishra 13
I am able to get values of both country and state as suggested here.
https://help.salesforce.com/apex/HTViewSolution?id=000212327&language=en_US
Abhilash Mishra 13Abhilash Mishra 13
Hi finally i was able to it by using , user fields. implmenting user.countrycode and user.statecode
how ever you have to use additonal JS as the value stored will be name of countries and states. not the codes. 
This was selected as the best answer
Vinay G 25Vinay G 25
Hi abhilash the solution which you have provided is not enough to map state based on country selected.
Do you have more information this?
Piotr Gajek 19Piotr Gajek 19

Hello everyone,

I know it's quite an old question, but I found a solution, so new people can know :)

More details you can find here: https://beyondthecloud.dev/blog/country-state-dependent-picklist-in-apex

Code (ready to use):

public with sharing class AddressSelectorUiApi {

    public static Map<String, List<Object>> getAddressSettings() {
        return new Map<String, List<Object>>{
            'countries' => getCountries(),
            'states' => getStates()
        };
    }

    public static List<Country> getCountries() {
        Map<String, Object> uiApiResponse = (Map<String, Object>) JSON.deserializeUntyped(
            AddressSelectorUiApi.callSalesforceUiApi('/services/data/v54.0/ui-api/object-info/Account/picklist-values/012000000000000AAA/BillingCountryCode')
        );

        List<Country> countries = new List<Country>();

        for (Object countryObject : (List<Object>) uiApiResponse.get('values')) {
            Map<String, Object> country = (Map<String, Object>) countryObject;

            countries.add(new Country((String) country.get('label'), (String) country.get('value')));
        }

        return countries;
    }

    public static List<State> getStates() {
        Map<String, Object> uiApiResponse = (Map<String, Object>) JSON.deserializeUntyped(
            AddressSelectorUiApi.callSalesforceUiApi('/services/data/v54.0/ui-api/object-info/Account/picklist-values/012000000000000AAA/BillingStateCode')
        );

        Map<String, Object> countryToValidFor = (Map<String, Object>) uiApiResponse.get('controllerValues');

        Map<Integer, String> validForToCountry = new Map<Integer, String>();

        for (String countryIsoCode : countryToValidFor.keySet()) {
            validForToCountry.put((Integer) countryToValidFor.get(countryIsoCode), countryIsoCode);
        }

        List<State> states = new List<State>();

        for (Object stateObject : (List<Object>) uiApiResponse.get('values')) {
            Map<String, Object> state = (Map<String, Object>) stateObject;
            List<Object> validFor = (List<Object>) state.get('validFor');

            states.add(
                new State(
                    (String) state.get('label'),
                    (String) state.get('value'),
                    (String) (validFor.isEmpty() ? '' : validForToCountry.get((Integer) validFor[0]))
                )
            );
        }

        return states;
    }

    public static String callSalesforceUiApi(String endpoint) {
        String restApiUrl = URL.getOrgDomainUrl().toExternalForm() + endpoint;

        HttpRequest request = new HttpRequest();
        request.setEndpoint(restApiUrl);
        request.setMethod('GET');
        request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());

        HttpResponse response = new Http().send(request);

        if (response.getStatusCode() == 200) {
            return response.getBody();
        }

        return '';
    }
}