Advertisement
CRM Hygiene

How to standardize country and state values in a Salesforce export

“USA”, “U.S.A.”, “United States” and “US” are one country and four different values in the same export column. A five-step cleanup — audit, map, transform, validate, document — that gives every country and state one written form.

Blank map of the world, Robinson projection
A blank world map — every country on it has more than one name in a raw export. Map by Canuckguy and contributors, public domain, via Wikimedia Commons.

The export on your screen

Open an account export from almost any Salesforce org that has been running for a few years, filter to the billing country and billing state columns, and the result usually looks like the sample below. These are eight invented rows: same customers, same places, spelled half a dozen ways.

AccountBilling countryBilling state
Acme CorpUSACalifornia
Beta LtdU.S.A.Calif.
Gamma IncUnited StatesCA
Delta GmbHDEBavaria
Epsilon SAGermanyBavaria
Zeta PtyAustraliaNew South Wales
Eta CoAUNSW
Theta LLCUK(blank)

Nothing here is a typo in the usual sense. Every row was typed by a person who meant the right place. The damage is structural: three spellings of one country, a state written out in one row and abbreviated in the next, a two-letter code doing double duty as a country in one row and a state in another, and a blank where a territory report expects a value. Group this export by country and the United States splits into three groups. Filter it for California and the abbreviated rows drop out. Feed it to a territory assignment or a tax calculation and the mismatches turn into misrouted records. Standardizing country and state values means fixing the values once, in a controlled pass, and then making it hard for the mess to come back. The five steps below do that in the order practitioners use: audit, map, transform, validate, document.

Step 1: audit the export before touching a value

Start by exporting the full set of records you intend to fix, with the record ID in the first column and the country and state columns beside it. Work on a copy, never on the live export, and keep the original file untouched so you can always compare before and after. Then count distinct values. In a spreadsheet, a pivot table or a distinct-values count on the country column will show the scale of the problem in one glance: an org that sells in twenty countries should show roughly twenty country values, and if it shows sixty, the extra forty are variants. Do the same for the state column, grouped by country, because a state value only means something next to its country. The audit is also where you find the four defect types that drive the rest of the work: spelling and format variants ("USA" against "U.S.A.", "Calif." against "California"); the swapped field, a state name in the country column or the reverse; the ambiguous code, such as "CA" meaning California in a state column and Canada in a country column; and the blank or placeholder, empty states or countries filled with "N/A". Count each type and write the counts down. Those counts become the success measure for step 4: the audit numbers are the before picture, and the validation pass has to show them at zero.

Step 2: map every variant to one canonical value

Mapping is the decision step, and it happens in a table, not in your head. Build a mapping sheet with three columns: the value as it appears today, the canonical value it should become, and the code that the canonical value carries. Every distinct value from the audit gets one row. Nothing is fixed by editing records directly at this stage; the mapping table is the single place where judgment lives, so it can be reviewed, corrected, and reused.

Value as exportedCanonical countryCanonical stateCode pair
USAUnited States—US
U.S.A.United States—US
United StatesUnited States—US
Calif.United StatesCaliforniaUS-CA
CA (in a state column)United StatesCaliforniaUS-CA
NSWAustraliaNew South WalesAU-NSW
UKUnited Kingdom—GB
DEGermany—DE

Three decisions in this table deserve explicit rules, because they are the ones that cause repeat arguments. First, pick one canonical label per country and per state and write it down: "United States" rather than "USA", "California" rather than "Calif.". Second, decide the code standard and use it everywhere: two-letter country codes with the matching state code, so "US-CA" always means California in the United States and never anything else. Third, resolve every ambiguous value with its row context, not with a global find and replace. "CA" in a state column next to a United States country becomes California; "CA" in a country column becomes Canada. The mapping table is where those calls are recorded, and rows that cannot be resolved from the export alone go on a hold list for a human to check against the account's other fields, such as the postal code or the city, rather than being guessed.

The same map-then-transform method works for the account column, too: see how to standardize company names in a CSV so that "Acme Inc", "ACME Incorporated" and "Acme" land as a single company record in Salesforce.

Step 3: transform the data in a staging file

Transformation is mechanical once the map exists, and it should happen outside Salesforce first. In the staging spreadsheet, add canonical country and canonical state columns, and fill them with a lookup against the mapping table: each exported value finds its canonical row, and anything without a match is flagged rather than silently passed through. Clean whitespace and casing as part of the same pass, so " california " with leading spaces and "CALIFORNIA" in capitals both land on "California". Leave the original columns in the file, untouched, next to the new canonical columns. If a canonical value is wrong, the original is still there to re-derive from, and if anyone questions a change, the mapping table shows exactly which rule produced it.

Globe and books illustration
Globe and books, by AlphaZeta, CC0, via Wikimedia Commons.
AccountBilling country, beforeBilling country, afterBilling state, beforeBilling state, after
Acme CorpUSAUnited StatesCaliforniaCalifornia
Beta LtdU.S.A.United StatesCalif.California
Gamma IncUnited StatesUnited StatesCACalifornia
Delta GmbHDEGermanyBavariaBavaria
Epsilon SAGermanyGermanyBavariaBavaria
Zeta PtyAustraliaAustraliaNew South WalesNew South Wales
Eta CoAUAustraliaNSWNew South Wales
Theta LLCUKUnited Kingdom(blank)(held for review)

Two rows in that table need a note. Delta and Epsilon already agree after mapping, which is the point: the cleanup reveals that they were the same place all along. Theta keeps its blank state, flagged for review, because inventing a state for a United Kingdom account would be fabricating data. A blank that survives the transform is a fact about the record, and it belongs on a follow-up list for the account owner, not silently filled with a guess.

When the staging file is clean, load it back, matching on the record ID from the export and mapping the canonical columns to the country and state fields. Run the load in a sandbox or on a small test batch first: a few hundred records will surface mapping mistakes while they are still cheap to reverse. Which tool runs the load is covered below.

Step 4: validate the result and prove the mess is gone

Validation is a second audit, run against the reloaded data, and it has to be independent of the transform: do not check the file you loaded, check what Salesforce now holds. Export the same records again and run the same distinct-value counts from step 1. The country column should now show one value per country, the state column one value per state within each country, and the four defect counts from the audit should read zero. Then run the checks that a count alone cannot catch. Every state must belong to its country: a row with country "Germany" and state "California" is a combination error, and it usually means a swapped field survived the mapping. Every country value must be one of the values your org has agreed to accept, with no stragglers like "U.S.A." left behind by a lookup miss. Every record that was held for review must still be on the hold list, untouched, and not quietly overwritten with a default.

Salesforce runs its own validation at this point, and it is worth understanding before the load rather than discovering it in an error file. Validation rules run on records before they are imported, and records that fail validation are not imported. That has a practical consequence for a cleanup load: a rule that rejects blank states will reject every held-for-review record in the batch, so either the rule is deactivated for the load window or the held records are excluded from the file. The picklist settings matter too. If a state or country field is an unrestricted picklist, the Data Import Wizard uses the value in the import file, even if it is new. If it is a restricted picklist, the wizard uses the picklist default value for an unmatched entry instead. A restricted picklist can therefore silently replace a carefully mapped value with a default, which is why the validation export after the load is the real test: it shows what Salesforce stored, not what the file contained.

Step 5: document the standard so it survives the next hire

A cleanup with no written standard lasts until the next import. The documentation step is short and it is the part that makes the previous four steps permanent. Publish the canonical list: every country and state your org uses, with its label and its code, in one page the team can reach. Publish the mapping table from step 2 alongside it, so the next person who finds "U.S.A." in a file does not have to decide again what it means. Name an owner for the list and a cadence for re-auditing it, even a quarterly distinct-value count on the country and state columns. Record the judgment calls: which label won for each country, how ambiguous codes are resolved, and what happens to records that cannot be resolved. A future admin who can read those decisions can keep the standard; one who cannot will reintroduce the variants.

Salesforce's own picklists: stop the mess at the point of entry

Cleaning an export fixes history. Salesforce's state and country/territory picklists fix the future, by replacing free-text address fields with selections from a predefined list. With the picklists enabled, a user picks a country first and then picks from the states that belong to that country, instead of typing either value. The feature ships with 235 countries and territories by default, and with the states and provinces of the United States, Canada, Australia, Brazil, China, Japan, India, Ireland, Italy, and Mexico. Enabling it also populates ISO code fields on records, so integrations can match on codes rather than on display labels.

Setting the picklists up follows a fixed sequence in Setup. First, configure which countries and territories the org uses: from Setup, enter "State and Country/Territory Picklists" in the Quick Find box, select it, and click "Configure States, Countries, and Territories". Each country can be set to Active, which makes it available through the Metadata API so records containing it can be imported, and Visible, which makes it available to users in Salesforce; a country must be active before it can be made visible. Clicking Edit on a country opens its states or provinces for the same treatment. An optional default country can be set under Picklist Settings, and it will automatically populate the country picklist on new records while still letting users choose another value. Configuring the lists is not strictly required before enabling the picklists, but Salesforce Help recommends it, because the configuration step is what keeps existing data and customizations consistent.

The actual switch is a single action on the same Setup page: click "Enable Picklists for Address Fields". The picklists are available to users immediately, but Salesforce notes that it can take some time to populate the ISO code fields on existing records, and a user who edits a state or country before the code field is populated is prompted to select a code value. That timing matters for a cleanup project: convert the existing values first, using the five steps above, and only then enable the picklists.

Validation rules: the second lock on the door

Picklists standardize what users can select. Validation rules standardize what the system will accept, and they apply to imports and integrations as well as to typed entry. A validation rule verifies that the data a user enters in a record meets the standards you specify before the record can be saved. Each rule holds a formula or expression that evaluates the data in one or more fields and returns True or False, plus an error message that displays when the formula returns True for an invalid value. Defining one follows a short path: from the management settings for the relevant object, go to Validation Rules, click New, enter the rule properties, and click Check Syntax to test the formula before saving. The permission needed to define or change validation rules is Customize Application.

Two rules cover most of the country and state cases. The first requires a country code on every account that has an address: an error condition formula of ISBLANK(BillingCountryCode) blocks the save with a message naming the missing field whenever the country code is empty. The second catches the combination error, a state without a country: AND(NOT(ISBLANK(BillingStateCode)), ISBLANK(BillingCountryCode)) fires only when a state code is present and the country code is not, which is exactly the swapped-field pattern the audit finds. Keep the error messages specific, naming the field and the value expected. Test each rule in a sandbox with the same messy values from the audit, including blanks, lowercase codes, and a state paired with the wrong country, and confirm that each one is stopped with a readable message. Salesforce Help also warns to test rules before activating them, because conflicting rules on the same field can leave a record unsavable.

Picklists and validation rules do different jobs and both are needed. A picklist prevents a user from typing "U.S.A."; a validation rule prevents an integration from loading a record with a state and no country. Neither verifies that the address is real: a correctly formatted country and state can still sit next to a wrong street or postal code.

Choosing the import tool for the load

The cleanup load is also a good moment to pick the right import tool deliberately. The Data Import Wizard is an in-browser wizard, available in all editions except Personal and Database.com, that imports accounts, contacts, leads, solutions, campaign members, and custom objects, up to 50,000 records at a time. Data Loader is an external application for bulk import or export, available in Enterprise, Unlimited, Performance, Developer, and Database.com editions, that handles between 5,000 and 5 million records and can insert, update, delete, or export records. For a country and state cleanup, the decision is usually volume and direction: under 50,000 records and one-way in, use the wizard; over that, or when the job needs deletes or exports, use Data Loader. Whichever tool runs the load, the staging file and the mapping table stay the same, and the validation export after the load is what proves the tool did what the map said.

What done looks like

A standardized export is easy to recognize. Group by country and each country appears once. Filter by a state and every row that belongs to that state is present, with no stragglers hiding under an abbreviation. An import of new records cannot reintroduce "U.S.A." because the picklist will not offer it, and a record with a state and no country cannot be saved because the validation rule will not allow it. The mapping table sits in documentation with an owner and a review date, and the next audit starts from the counts the last one ended with. The surgery is the audit, the map, the transform, the validation, and the document, in that order, and the order is what makes it stick.

Sources

Know someone who needs this? Share it.

Advertisement