Created
January 16, 2018 17:50
-
-
Save mchang-r7/bf1ae7d574087a6aa67cbfc4ed55533c to your computer and use it in GitHub Desktop.
Export state and country picklists as CSV files from Salesforce by using the Chrome Developer Console to parse the XML in Address.settings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Step 1, store the XML in a string | |
// Replace the string with the actual, full XML content copied as-is | |
// from Address.settings | |
var xml = '...'; | |
// Step 2, parse the XML | |
// See https://www.w3schools.com/xml/xml_parser.asp | |
var parser = new DOMParser(); | |
var xmlDoc = parser.parseFromString(xml, "text/xml"); | |
// Step 3, traverse the xml | |
var addressSettings = xmlDoc.getElementsByTagName('AddressSettings')[0]; | |
var countriesAndStates = | |
addressSettings.getElementsByTagName('countriesAndStates')[0]; | |
var countries = countriesAndStates.getElementsByTagName('countries'); | |
// Initialize the list of state and country codes | |
var countryCsv = [['Country', 'CountryCode']]; | |
var stateCsv = [['CountryCode', 'State', 'StateCode']]; | |
var escape = function(value) { | |
return '"' + value + '"'; | |
}; | |
for (var i = 0; i < countries.length; i++) { | |
// Work on the current country | |
var country = countries[i]; | |
var countryLabel = country.getElementsByTagName('label')[0].textContent; | |
var countryCode = country.getElementsByTagName('isoCode')[0].textContent; | |
// Add the country to the countries CSV | |
countryCsv.push([escape(countryLabel), escape(countryCode)]); | |
// Get the list of states for the country | |
var states = country.getElementsByTagName('states'); | |
// Populate the states CSV | |
for (var j = 0; j < states.length; j++) { | |
// Work on the current state | |
var state = states[j]; | |
var stateLabel = state.getElementsByTagName('label')[0].textContent; | |
var stateCode = state.getElementsByTagName('isoCode')[0].textContent; | |
// Add the state to the sates CSV | |
stateCsv.push([ | |
escape(countryCode), | |
escape(stateLabel), | |
escape(stateCode) | |
]); | |
} | |
} | |
// Step 4, print the CSV contents | |
var dumpCsv = function(csv) { | |
csvLines = []; | |
csv.forEach(function(row) { | |
csvLines.push(row.join(',')) | |
}) | |
text = csvLines.join('\n'); | |
return text; | |
}; | |
prompt('Select all, then copy', dumpCsv(countryCsv)); | |
prompt('Select all, then copy', dumpCsv(stateCsv)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment