Dynamics CRM 2011 Javascript ZipCode/Postal Code Lookup

By - May 25, 2012

Recently, I needed to write some javascript to do a zip code/postal code lookup for validation purposes and it also needed to work for international. I also added the ability to populate the city and state fields (may as well). Below is the code. Pay special attention to the comments section. In order for the code to work (and to not be prompted), you will need to make some changes to your IE security settings. This got me thinking, this particular bit of code utilizes ActiveX. With Dynamics CRM 2011 R8 right around the corner and providing cross browser support, this particular type of javascript will not function in non-IE environments. If I helped you here by providing this code, then help me by providing some non-ActiveX (non IE) code for the relevant sections (and we’ll be even). I’m being a little lazy because it’s late and this will work for now.

function ZipCodeLookup(zipFieldName, cityFieldName, stateFieldName, countryFieldName) {
This code originally written in June 2011 by Bill Caldwell (dynamicscrmdenver.com) from McGladrey
This code was modified in October 2011 by Bill C to consume the latest yahoo geocode services
This code was modified on 5/25/2012 by Bill C to provide international postal code functionality
The code requires certain IE security settings must be changed (under miscellaneous, change “access data sources across domains” to ‘enable’.
The user is not required to enter the country (will default to US)
usage: ZipCodeLookup(“address1_postalcode”, “address1_city”, “address1_stateorprovince”, “address1_country”);
// Verify that the field is valid

var zip = Xrm.Page.getAttribute(zipFieldName).getValue();

if (zip != null) {

// get url to Yahoo geocode service

var url = http://where.yahooapis.com/geocode?appid=MicrosoftCRM;

//pass country

var country = Xrm.Page.getAttribute(countryFieldName).getValue();

if (country == null) {
country = “United States”;

//url += “&country=United States&postal=” + zip;
url += “&country=” + country + “&postal=” + zip;

var httpRequest = new ActiveXObject(“Msxml2.XMLHTTP”);

// send request and store response
httpRequest.Open(“GET”, url, false);
var httpResponse = httpRequest.responseXML;
// populate city, state and country in CRM if zip is valid
if (httpResponse.selectSingleNode(“ResultSet/Result/city”) == null) {
alert(“ZipCode cannot be found”);
// if zip code is not found set fields to null
else {
else {
// if zip code is not entered, set fields to null

Receive Posts by Email

Subscribe and receive notifications of new posts by email.