Recently I was asked by a customer to populate city, county and state depending on the zip code for accounts and contact. It involved lot of research and a simple java script method worth sharing with the community to save lot of time. I implemented this for accounts and contacts but the same method can be used for any custom entity with address fields. I am using this method for United States but the country parameter in the URL can be modified to make it work for any country. Also, I am just using city, state and county from this method but much more information can be extracted like latitude, longitude etc.
The following script needs to be added to onchange event of zip code field on the form:
function ZipCodeLookup() {
var zip = Xrm.Page.getAttribute(“address1_postalcode”).getValue();
if (zip != null) {
// get url to Yahoo geocode service
var url = “http://where.yahooapis.com/geocode?appid=MicrosoftCRM”;
//alert(country);
//url += “&country=Country&postal=” + zip;
url += “&country=United States&postal=” + zip;
var httpRequest = new ActiveXObject(“Msxml2.XMLHTTP”);
// send request and store response
httpRequest.Open(“GET”, url, false);
httpRequest.Send();
var httpResponse = httpRequest.responseXML;
//alert(httpResponse.selectSingleNode(“ResultSet/Result/city”).text)
// populate city, state and country in CRM if zip is valid
if (httpResponse.selectSingleNode(“ResultSet/Result/city”) != null) {
Xrm.Page.getAttribute(“address1_city”).setValue(httpResponse.selectSingleNode(“ResultSet/Result/city”).text);
Xrm.Page.getAttribute(“address1_stateorprovince”).setValue(httpResponse.selectSingleNode(“ResultSet/Result/statecode”).text);
Xrm.Page.getAttribute(“address1_county”).setValue(httpResponse.selectSingleNode(“ResultSet/Result/county”).text);
}}
}
Thiѕ text iѕ worth everyοne’s attention. ʜow ϲan I find
out morе?
Hi Ashish,
I tried using your code above but I am getting a “permission denied” error. I am not sure if the permissions are on the CRM side or on the geocode side, as I am logged in to CRM as the sys admin. Any ideas / suggestions would be greatly appreciated.
Thanks,
Dhruv
Hi,
While trying to use the code above, I get a permission denied error. I am logged in as the System Administrator. Any ideas what permissions it is asking for?