1

I want to check if state exists or not with a country in State and Country Picklist feature supported by Salesforce. I want to do this in Apex. How can I do this?

  • describeSObject is exposed in Apex now, right? If you describe an entity that has an address field, the state values should be in the field metadata, along with their controlling (country) values. – Carolyn Grabill Jul 23 '15 at 5:52
  • unfortunately controlling country value is not included when you describe state picklist – Ian Roque Jul 23 '15 at 16:59
0

Here is a similar working example as below. The 2nd pick-list value is selected based on the first pick-list through Apex. You can add your Apex logic inside spin() method. Hope this will helps.

VF Page

<apex:page controller="loadingSpinnerCtrl">
    <apex:form id="form">
        <apex:selectList id="selected_list" value="{!dropdown1}" required="false" size="1">
            <apex:selectOption itemvalue="None" itemLabel="--None--"/>
            <apex:selectOption itemvalue="1" itemLabel="First"/>
            <apex:selectOption itemvalue="2" itemLabel="Second"/>
            <apex:actionSupport event="onchange" reRender="form" action="{!spin}" status="status"/>
        </apex:selectList>
        <apex:actionStatus id="status">
            <apex:facet name="start">
                <div>Loading Please wait........</div>
            </apex:facet>
        </apex:actionStatus>
        <apex:selectList id="dropdown2"  required="false" size="1">
            <apex:selectOption itemvalue="None" itemLabel="--None--"/>
            <apex:selectOptions value="{!items}" />
        </apex:selectList>        
    </apex:form>
</apex:page>

Controller

public class loadingSpinnerCtrl {
    public String dropdown1{get; set;}
    public List<SelectOption> options;
    public void spin() {
        if(dropdown1 == '1'){
            options = new List<SelectOption>();
            options.add(new SelectOption('1','Value 1'));
            options.add(new SelectOption('2','Value 2'));
            options.add(new SelectOption('3','Value 3'));

        }else if(dropdown1 == '2'){
            options = new List<SelectOption>();
            options.add(new SelectOption('1','Case 1'));
            options.add(new SelectOption('2','case 2'));
            options.add(new SelectOption('3','case 3'));

        }    
        long now = datetime.now().gettime();
        while(datetime.now().gettime()-now<5000); // Busy loop for 5000 ms
    }
    public List<SelectOption> getItems() {
        return options;
    }    
}
  • This answer looks unrelated to the question. – Keith C Jul 22 '15 at 12:53
  • @KeithC If he selects a country first and then a state, assuming he's done a call to get the values, this would seem to me to do the trick or at least establish the pattern. – crmprogdev Jul 22 '15 at 13:26
  • @KeithC I agree with crmprogdev, I want to give Ian an idea on how to communicate with Apex with onchange event of a picklist. – Saroj Bera Jul 22 '15 at 13:29
  • The call to get the values is the question. I agree with Keith C. – Carolyn Grabill Jul 23 '15 at 5:51
0

Get the list of countries and states as described here

country code - country name

Schema.DescribeFieldResult fieldResult = 
User.Countrycode.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
System.debug('Picklist::'+ple);
for( Schema.PicklistEntry f : ple){
System.debug(f.getLabel() +'::'+ f.getValue());
}

state code - state name

Schema.DescribeFieldResult fieldResult = User.statecode.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
System.debug('Picklist::'+ple);
for( Schema.PicklistEntry f : ple){
System.debug(f.getLabel() +'::'+ f.getValue());
}

The problem is that we don't know which state belongs to which country. That is not as easy to solve, but some solutions are available here

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.