locked
How to populate City and state with Zip RRS feed

  • Question

  • So I'm experimenting with City and State entity lookups for Account.  I've found threads on this and it works ok.
    Now this is where it I'm not sure the best method.

    Should I add a third entity with Zips in it? or should I just attach the Zip to the City Entity?
    Then I want to be able to complete City and State on an Account/Contact by merely entering in the zip.  I would imagine I could use an OnChange event and a Soap call, just not sure if the Zip should be a separate entity.
    Withers
    Monday, August 3, 2009 3:58 PM

Answers

  • David -

    there's an alternate way - you can use the zip code to retrieve the city/state using Yahoo's geocode webservice.-
    The original posting was from Invoke - from Aaron Elder, I believe -

    Here's a sample that could be added to the zipcode's onchange field:  (This version was modified by Grzegorz Kalek:)


    if (crmForm.address1_postalcode.DataValue != null) {
        // form and send request to Yahoo Geocode services  
        var sUrl = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=MicrosoftCRM";
        sUrl += "&zip=" + crmForm.address1_postalcode.DataValue;
        var oXmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");
    
        // send request and store response
        oXmlHTTP.Open("GET", sUrl, false);
        oXmlHTTP.Send();
    
        var oXmlDoc = oXmlHTTP.responseXML;
        // populate city, state and country in CRM if zip is valid
        //alert(oXmlDoc.selectSingleNode("ResultSet/Result/City").text);
    
        crmForm.address1_city.DataValue = toProperCase(oXmlDoc.selectSingleNode("ResultSet/Result/City").text);
        crmForm.address1_stateorprovince.DataValue = oXmlDoc.selectSingleNode("ResultSet/Result/State").text;
        crmForm.address1_country.DataValue = oXmlDoc.selectSingleNode("ResultSet/Result/Country").text;
    }
    else {
        // if zip code is not found set fields to null
        crmForm.address1_city.DataValue = null;
        crmForm.address1_stateorprovince.DataValue = null;
        crmForm.address1_country.DataValue = null;
    }
    
    // converts data to proper case
    function toProperCase(s) {
        return s.toLowerCase().replace(/^(.)|\s(.)/g, 
            function($1) { return $1.toUpperCase(); });
    }
    

    Scott Sewell, CustomerEffective | http:\\blog.CustomerEffective.com | Twitter:@ScottSewell
    • Marked as answer by David Withers Monday, August 3, 2009 5:32 PM
    Monday, August 3, 2009 4:31 PM
    Moderator

All replies

  • David -

    there's an alternate way - you can use the zip code to retrieve the city/state using Yahoo's geocode webservice.-
    The original posting was from Invoke - from Aaron Elder, I believe -

    Here's a sample that could be added to the zipcode's onchange field:  (This version was modified by Grzegorz Kalek:)


    if (crmForm.address1_postalcode.DataValue != null) {
        // form and send request to Yahoo Geocode services  
        var sUrl = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=MicrosoftCRM";
        sUrl += "&zip=" + crmForm.address1_postalcode.DataValue;
        var oXmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");
    
        // send request and store response
        oXmlHTTP.Open("GET", sUrl, false);
        oXmlHTTP.Send();
    
        var oXmlDoc = oXmlHTTP.responseXML;
        // populate city, state and country in CRM if zip is valid
        //alert(oXmlDoc.selectSingleNode("ResultSet/Result/City").text);
    
        crmForm.address1_city.DataValue = toProperCase(oXmlDoc.selectSingleNode("ResultSet/Result/City").text);
        crmForm.address1_stateorprovince.DataValue = oXmlDoc.selectSingleNode("ResultSet/Result/State").text;
        crmForm.address1_country.DataValue = oXmlDoc.selectSingleNode("ResultSet/Result/Country").text;
    }
    else {
        // if zip code is not found set fields to null
        crmForm.address1_city.DataValue = null;
        crmForm.address1_stateorprovince.DataValue = null;
        crmForm.address1_country.DataValue = null;
    }
    
    // converts data to proper case
    function toProperCase(s) {
        return s.toLowerCase().replace(/^(.)|\s(.)/g, 
            function($1) { return $1.toUpperCase(); });
    }
    

    Scott Sewell, CustomerEffective | http:\\blog.CustomerEffective.com | Twitter:@ScottSewell
    • Marked as answer by David Withers Monday, August 3, 2009 5:32 PM
    Monday, August 3, 2009 4:31 PM
    Moderator
  • One Snag:
    Browser warning:
    This page is accessing information that is not under its control. This poses a secrutiy risk. Do you want to continue?

    This pops up every time.  I'm going to ask our domain admin to add this site to trusted, but we'll see.


    Withers
    Monday, August 3, 2009 5:38 PM
  • Yes - since it's passing data outside the CRM control, that warning is shown. -  - I don't think adding it as 'trusted' will prevent the warning since the data is still being passed to/retreived from an external site.


    Scott Sewell, CustomerEffective | http:\\blog.CustomerEffective.com | Twitter:@ScottSewell
    Monday, August 3, 2009 5:41 PM
    Moderator
  • You are correct, I had to add it to intranet.  Then the warning goes away. 

    Withers
    Monday, August 3, 2009 5:43 PM
  • OK i'm getting an Error:Permission Denied

    On the OnChange Event for my test user

    Withers
    Monday, August 3, 2009 7:19 PM
  • Hi Withers,

    Did you get the Permission Denied error resolved? I only seem to get it when using the Outlook client.  If i close Outlook and close and open the web browser, all seems to work fine, but as soon as i open Outlook again i get the error.

    TD
    Sunday, October 25, 2009 2:19 AM
  • No we did not as we are almost exclusively a CRM via Outlook shop.


    Withers
    Monday, October 26, 2009 4:09 PM
  • Of course, if you want to avoid having to deploy Yahoo to all your users' Trusted Sites or Intranet zones, you could make the webservice call from the CRM server with Plugin code.  Though I do appreciate the desire for something a little more instant.  Another warning you'll have to consider, if your CRM is secured with SSL, is the passing of data between secured and unsecured domains.
    Dave Berry
    Monday, October 26, 2009 4:56 PM
    Moderator