Pages

Wednesday, December 9, 2015

Salesforce: State and Country Picklists

By default, State and Country is textbox field, where user can enter any data. Some organization hire developer to write trigger to validate the data entered is valid. Another option is to use AppExchange product such as AddressTools Premium or AddressTools Free.

But if you are not aware, Salesforce actually have something called State and Country Picklists, this is out of the box feature to change textbox field to picklist for both State and Country, this is applicable for shipping, billing, mailing, and “other” address fields in standard object:
  • account
  • campaign members
  • contact 
  • contract
  • lead
  • person accounts
  • quotes
  • service contracts 

Benefit
When State and Country Picklists is activated, users will be not able manually to type into State and Country, but they have to select from picklist. This will make your data clean, e.g. United States maybe enter as USA, U.S., United States America, this will make you will be difficult to analyze data correctly.


How to activate this?
1. Navigate to SetupData ManagementState and Country Picklists

2. Click Configure states and countries in step 1
  • You can set default country, in case your business mostly only happened within a country. 
  • By default Salesforce will provide 239 countries and some of them have States/Provinces. For countries that you know should have State, but not available, click Edit link on that country, click New State button to add State. This manual process will take a lot of time when you need to enter many State not available out of the box.
  • You also can modify the Country Name and Integration Name. Integration Name must be unique, Salesforce uses this value to integrate new picklist values with former text-only values. It is not case sensitive. When you add new country, Country Code must be unique and you cannot modify standard country code.
  • To hide a country visible for user to select in picklist, unselect Visible checkbox

3. Click Scan Now in step 2
  • You will receive 2 emails: one regarding affected address data and one regarding affected customizations. 
  • For customizations scan result, you need to download a text file from document, this will include where State and Country used in your org: 
    • Apex Class
    • Visualforce Page
    • Apex Trigger
    • Assignment Rule
    • Auto-Response Rule
    • Email Template
    • Escalation Rule
    • Field Set
    • List View
    • Report
    • Validation Rule
    • Custom Button or Link
    • Workflow Rule
    • Matching Rule
For reports and list views, it will not include reports and list views in personal folder or visible only to a user, except it owned by user who run the scanner.
4. After get two emails above, click Convert Now in step 3
For address (state and country), you need to select the data and select a value from Change To picklist, If you do not know the right value at that moment, you can map it to "Unknown value". Users user open the record, they will see original state and country (not Unknown value), but they will encounter error to save, unless they change the state or country to a valid value before saving. You should only map to "Unknown value" when you do not know the correct value.
This step will take a lot of time to clean up invalid data manually.

5. Once conversion process finish, you will be able to click Enable button at step 4 Turn on state and country picklists.

Notes:
1. If you change a country name at step 1, the original name will be shown in Change To picklist when you convert state or country to picklist, just select the original name, and it will use value renamed, example: Viet Nam to Vietnam. This maybe a Salesforce defect, but not a blocker.



2. Notice two new fields created for all objects with standard address field, which is BillingCountryCode and BillingStateCode.

Before enable state and country picklist:

After enable state and country picklist:



3. From the last screenshot above taken from SOQL query, invalid value converted to 'Unknown value' will not have value in the Code field. For valid values, code will be auto populated with the Country Code and State Code.

4. When we run report, we can see the old value with suffix (text only)



5. When you open existing report with state or country, it will prompt you to update the report with new picklist field.

But, if you do not have permission to edit the report, here is the screenshot.

Effect with Insert / Update (integration or mass loader)
  • So which field should I use? BillingCountry or BillingCountryCode or both? You can provide only one value, either name or code, and the other value will be auto populated by the system. But if you populate both for some reason, make sure the code is align with the country name, otherwise the insert/update process will be failed. The same goes for State.
  • The country / state name or code must be exist in the picklist value defined, otherwise the insert/update process will be failed. 
  • When you insert / update State, make sure that state is available for that particular country, otherwise the process will failed.

So this is how it looks like when you had State and Country Picklists enabled. Notice both Country and State is a picklist, and State picklist value is depend on Country selected.



Reference: