In salesforce, Under Data Management setup you can find State and Country Picklists option. By using this feature, you can setup countries and state options as picklist. Once if you enabled, you can see like below,
The below image shown in standard edit page of Account.
You may have a scenario like you need to display these field in Visualforce page. Referring address field like below won't show as a picklist in visualforce page.
The below image shown in standard edit page of Account.
You may have a scenario like you need to display these field in Visualforce page. Referring address field like below won't show as a picklist in visualforce page.
- <apex:page standardController="Account">
- <apex:form >
- <apex:pageBlock>
- <apex:pageBlockSection >
- <apex:inputField value="{!Account.BillingCountry}"/>
- <apex:inputField value="{!Account.BillingState}"/>
- </apex:pageBlockSection>
- </apex:pageBlock>
- </apex:form>
- </apex:page>
The above code will show only as input field not as picklist field.
To resolve this issue, you need to use StateCode and CountryCode fields in order to show as picklist in visualforce page. Let see the below working code on different objects.
Referring Address fields in Contact Object:
- <apex:page standardController="Contact">
- <apex:form >
- <apex:pageBlock>
- <apex:pageBlockSection title="Contact Mailing Address">
- <apex:inputField value="{!Contact.MailingCountryCode}"/>
- <apex:inputField value="{!Contact.MailingStateCode}"/>
- </apex:pageBlockSection>
- <apex:pageBlockSection title="Other Address">
- <apex:inputField value="{!Contact.OtherCountryCode}"/>
- <apex:inputField value="{!Contact.OtherStateCode}"/>
- </apex:pageBlockSection>
- </apex:pageBlock>
- </apex:form>
- </apex:page>
Referring Address fields in Account Object:
- <apex:page standardController="Account">
- <apex:form >
- <apex:pageBlock>
- <apex:pageBlockSection title="Account Billing Address">
- <apex:inputField value="{!Account.BillingCountryCode}"/>
- <apex:inputField value="{!Account.BillingStateCode}"/>
- </apex:pageBlockSection>
- <apex:pageBlockSection title="Account Shipping Address">
- <apex:inputField value="{!Account.ShippingCountryCode}"/>
- <apex:inputField value="{!Account.ShippingStateCode}"/>
- </apex:pageBlockSection>
- </apex:pageBlock>
- </apex:form>
- </apex:page>
Referring Address fields in Lead Object:
- <apex:page standardController="Lead">
- <apex:form >
- <apex:pageBlock>
- <apex:pageBlockSection title="Lead Address">
- <apex:inputField value="{!Lead.CountryCode}"/>
- <apex:inputField value="{!Lead.StateCode}"/>
- </apex:pageBlockSection>
- </apex:pageBlock>
- </apex:form>
- </apex:page>
Note:
StateCode and CountryCode are always available on compound address fields, whether or not state and country picklists are enabled in your organization.
Reference:
Hi, I tried exactly you wrote, and get the selectlist displayed. When I try to edit and save, only 'Country' being update, but the 'state' still the same ......Any idea?
ReplyDeleteJust ensure, If any other process changing the state value, such as in trigger, workflow, process builder.
DeleteThanks Working.
ReplyDeletePerfect. Thanks
ReplyDeleteIn Lead, the state is not editable on visual force page as it is dependent on country, but when i choose the country from the picklist, the state is still non editable
ReplyDeleteplease help?
DeleteAbhishek,
DeleteSelect "United States" as country in visualforce page and check States are visible or not?
If it works, You need to configure the states for all other countries.
This can be configured at Set up --> State and Country Picklists -->
Configure states and countries --> Choose country and Add states.
I have done the same and values are displaying correctly , I have written a apex class to get the address data and check the address is correct or not and rendaring the pageblock country is displaying as given but state in not getting displayed as given value means displaying nothing. how to overcome this issue.
ReplyDeleteIf the same thing can work for User object? user.countrycode along with apex:inputfield to display country picklist.
ReplyDelete