Thursday 11 June 2015

Salesforce - How to Refer State and Country Picklists in Visualforce Page

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.

  1. <apex:page standardController="Account"> 
  2.     <apex:form >
  3.         <apex:pageBlock>
  4.             <apex:pageBlockSection >
  5.                 <apex:inputField value="{!Account.BillingCountry}"/>
  6.                 <apex:inputField value="{!Account.BillingState}"/>
  7.             </apex:pageBlockSection>
  8.         </apex:pageBlock>
  9.     </apex:form>
  10. </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:

  1. <apex:page standardController="Contact">
  2.     <apex:form >
  3.         <apex:pageBlock>
  4.             <apex:pageBlockSection title="Contact Mailing Address">
  5.                 <apex:inputField value="{!Contact.MailingCountryCode}"/>
  6.                 <apex:inputField value="{!Contact.MailingStateCode}"/>
  7.             </apex:pageBlockSection>
  8.            
  9.             <apex:pageBlockSection title="Other Address">
  10.                 <apex:inputField value="{!Contact.OtherCountryCode}"/>
  11.                 <apex:inputField value="{!Contact.OtherStateCode}"/>
  12.             </apex:pageBlockSection>           
  13.         </apex:pageBlock>       
  14.     </apex:form>
  15. </apex:page>


Referring Address fields in Account Object:

  1. <apex:page standardController="Account">
  2.     <apex:form >
  3.         <apex:pageBlock>
  4.             <apex:pageBlockSection title="Account Billing Address">
  5.                 <apex:inputField value="{!Account.BillingCountryCode}"/>
  6.                 <apex:inputField value="{!Account.BillingStateCode}"/>
  7.             </apex:pageBlockSection>
  8.            
  9.             <apex:pageBlockSection title="Account Shipping Address">
  10.                 <apex:inputField value="{!Account.ShippingCountryCode}"/>
  11.                 <apex:inputField value="{!Account.ShippingStateCode}"/>
  12.             </apex:pageBlockSection>   
  13.         </apex:pageBlock> 
  14.     </apex:form>
  15. </apex:page>

Referring Address fields in Lead Object:

  1. <apex:page standardController="Lead">
  2.     <apex:form >
  3.         <apex:pageBlock> 
  4.             <apex:pageBlockSection title="Lead Address">
  5.                 <apex:inputField value="{!Lead.CountryCode}"/>
  6.                 <apex:inputField value="{!Lead.StateCode}"/>
  7.             </apex:pageBlockSection>
  8.         </apex:pageBlock>
  9.     </apex:form>
  10. </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:


9 comments:

  1. 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?

    ReplyDelete
    Replies
    1. Just ensure, If any other process changing the state value, such as in trigger, workflow, process builder.

      Delete
  2. In 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

    ReplyDelete
    Replies
    1. please help?

      Delete
    2. Abhishek,

      Select "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.

      Delete
  3. 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.

    ReplyDelete
  4. If the same thing can work for User object? user.countrycode along with apex:inputfield to display country picklist.

    ReplyDelete

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...