19

I have played with the State & Country picklist value UI for adding them, and it works OK for small amounts, but it is not meant for batches. I have 760 additional states I need to add for various countries, and the UI takes about 30 seconds per state you add. I've been trying to edit the Address.settings XML file that you can pull down via Eclipse, and am finding that while I can edit new (custom) states I've manually added via the UI, I cannot add new states for whatever reason.

Each time it complains about the ISO Code, which to be fair probably aren't real ISO codes, but it is what the client wants to match their existing system. (Values are 1-2 digit numbers or 1-3 character letters).

Invalid iso code B for state Burgenland in country Austria

As I mentioned, if I added this to the UI and went in to change one of the associated pieces of metadata (say the tag), it works fine.

Here is an example of what I tried, based on how the states come back if you look at custom ones you add in the UI and how they show up in the XML.

<countries>
    <active>true</active>
    <integrationValue>Austria</integrationValue>
    <isoCode>AT</isoCode>
    <label>Austria</label>
    <orgDefault>false</orgDefault>
    <standard>true</standard>
    <states>
        <active>true</active>
        <integrationValue>Burgenland</integrationValue>
        <isoCode>B</isoCode>
        <label>Burgenland</label>
        <standard>false</standard>
        <visible>true</visible>
    </states>
    <visible>true</visible>
</countries>

UPDATE: Looks like I overlooked the note in the API docs about the Metadata API not supporting creates/deletes yet. http://www.salesforce.com/us/developer/docs/api_meta/Content/meta_addresssettings.htm

  • Did you ever find a more efficient/automated manner to add mass country/state picklist options? Thanks! – Doug Ayers Jun 9 '14 at 18:30
  • I haven't had to revisit state/country picklists since then, so I'm not sure if they came out with any metadata support for them. – Michael Welburn Jun 9 '14 at 20:04
  • The ISO Codes are the unique key it seems, so if you change the ISO code it means you're actually trying to create a new code rather than edit it, this then fails. Which means mass reorganisation of state and country picklist's is impossible as at Winter '15. – Steven Herod Dec 7 '14 at 4:31
  • What if you give random ISO code while creating a new one? – sfdcFanBoy May 19 '16 at 5:57
9

As of this time, there is no support for doing anything but mass editing of States you've already added manually to the system (no way to use the Metadata API to create/delete).

2

The best way I've found to do this is using the Selenium plugin for Firefox. It's a horrible hack, but it got the job done and has saved many hours typing thousands of values into multiple environments. You can generate a file by recording your actions in the browser, and then replay that file against a Salesforce session to insert the picklist values.

The recorded file is in HTML format, so you can use a text editor to generate it automatically. I loaded the state list I needed to import into Excel, and then generated the HTML I needed to import it into the org using excel concatenation functions.

Below is an example of the HTML I used, to import more simply insert repeated copies of the whole section within the <tbody> tag

<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta content="text/html; charset=UTF-8">
        <link rel="selenium.base" href="https://cs80.salesforce.com">
    </head>
    <body>
        <table cellpadding="1" cellspacing="1" border="1">
            <thead>
                <tr>
                    <td rowspan="1" colspan="3">New Test</td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>open</td>
                    <td>/i18n/ConfigureNewState.apexp?countryIso=****Country Code Here****&amp;setupid=AddressCleanerOverview</td>
                    <td></td>
                </tr>
                <tr>
                    <td>waitForElementPresent</td>
                    <td>id=configurenew:j_id1:blockNew:j_id9:nameSectionItem:editName</td>
                    <td></td>
                </tr>
                <tr>
                    <td>type</td>
                    <td>id=configurenew:j_id1:blockNew:j_id9:nameSectionItem:editName</td>
                    <td>****State Display Name Here****</td>
                </tr>
                <tr>
                    <td>type</td>
                    <td>id=configurenew:j_id1:blockNew:j_id9:codeSectionItem:editIsoCode</td>
                    <td>****State Code Here****</td>
                </tr>
                <tr>
                    <td>type</td>
                    <td>id=configurenew:j_id1:blockNew:j_id9:intValSectionItem:editIntVal</td>
                    <td>****State Integration Name Here****</td>
                </tr>
                <tr>
                    <td>click</td>
                    <td>id=configurenew:j_id1:blockNew:j_id9:activeSectionItem:editActive</td>
                    <td></td>
                </tr>
                <tr>
                    <td>waitForEditable</td>
                    <td>id=configurenew:j_id1:blockNew:j_id9:visibleSectionItem:editVisible</td>
                    <td></td>
                </tr>
                <tr>
                    <td>click</td>
                    <td>id=configurenew:j_id1:blockNew:j_id9:visibleSectionItem:editVisible</td>
                    <td></td>
                </tr>
                <tr>
                    <td>clickAndWait</td>
                    <td>id=configurenew:j_id1:blockNew:j_id43:addButton</td>
                    <td></td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

Beware, this solution is liable to break at any moment if Salesforce update the configuration UI. Also the reliability of the import varies with your network connection, and the speed you execute the script in Selenium (although someone with more detailed knowledge of Selenium may be able to fix this).

0

Here's a batch class that creates all ISO states:

  • approx 5,000 default state codes
  • it screen scrapes the 'Configure New State' form,
  • then posts back to the setup menu to create each picklist value:

https://github.com/bigassforce/statecodes

protected by Adrian Larson May 19 '16 at 16:13

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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