0

We have State and Country Picklist enabled in the org. I cannot find a way to find out the list of states based on the selected country. I need this in the lightning component.

I've used DescribeFieldResult in Apex to get the country and return it to the component in init. It is fine. But I cannot find any way to filter the States based on CountryCode.

Is there any direct way to do this? Or should I be implementing via adding those into Custom Settings.

<aura:attribute name="dynamicCountry" type="Object[]"></aura:attribute>
<aura:attribute name="dynamicState" type="Object[]"></aura:attribute>



<lightning:inputAddress
        aura:id="myaddress"
        addressLabel="Address"
        streetLabel="Street"
        cityLabel="City"
        countryLabel="Country"
        provinceLabel="Province/State"
    postalCodeLabel="PostalCode"
        street="1 Market St."
        city="San Francisco"
        country="US"
        countryOptions="{! v.dynamicCountry }"
        provinceOptions="{! v.dynamicState }"
        postalCode="94105"
        required="true"
    />

I get the countryoptions and provinceoptions populated from apex in init of lightning component. Both of them would get populated. The issue is filtering out the state based on country code in apex (and it always returns me the full list). I still need to write a change event on the inputAddress field to fire when country is changed (and call the function to populate the state). But I need to figure it out at Apex level how to achieve this.

Note: I also need to make this work with multiple languages. Hence I'm choosing to get it from the Country State picklist values.

marked as duplicate by Pranay Jaiswal, Raul, battery.cord, Brian Mansfield, Basti May 3 '18 at 13:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

EDIT

I later found this topic being discussed in this post. So definitely there's no way to use the standard lighting:inputAddress for this purpose.

I additionally tried using the lightning:inputField on Country/State, and that did not work either. So it looks like you may need to actually write some custom logic to maintain the dependency in the backend and handle onchange event with every country selection to populate the state values. You can find more details on this approach on the post referred above.


Looking at the documentation, it seems that while using the standard lightning:inputAddress, you may not be able to define the dependency between country and state out of the box. The values in here are just standard array values and they really don't have any dependency defined between them. You can possibly capture the onchange or onblur events and manipulate the values.

But I wouldn't recommend to go that route as it would require much more event handling and that you will always need to maintain and refer to a matrix defined for country/state mapping. As far as your apex scenario is considered, if you can address the dependency by handling the events, you will get the selected value without considering for anything in apex.

In your scenario I would rather recommend to use lightning:inputField which can render values based on the field type and also renders any dependency within picklist. The below excerpt is from the documentation:

Dependent picklists must be defined in your org before you can use them with lightning:inputField. Both controlling and dependent fields must be included in your component.

Note: I haven't really used lightning:inputField with Country/State picklist, but assume the behavior is similar as for any other dependent picklist.

In all other cases, you will need to resort to some customization.

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