none
validate zip codes

    Question

  • is there any way to validate a zip code of usa

    i mean i want to validate the city and state once the user enters the zip code

    i want to do this in account entity

    is it recommeded to save all the zip codes in the crm database
    Thursday, February 04, 2010 5:35 AM

Answers

  • Hi,
    my suggestion is:

    1. Create a Custom entity that holds all zip codes
    2. On the Account, Zip code field OnChange event Fetch the Custom entity to lookup the zip code

    The are allot of samples on how to Fetch. Below i have a sample i often use to return a City based on a zip code.

    //******************************************************************
    //* Function that returns a single value
    //*
    //* PARAMETERS
    //* #1 (strSearch)       : VALUE to search for
    //* #2 (strEntity)         : ENTITY to search within
    //* #3 (strAttributeX)   : ATTRIBUTE to return the value from
    //* #4 (strAttributeX)   : ATTRIBUTE to search within
    //*
    //*
    //* RETURNVALUES
    //* Returnvalue "ERROR GENERAL" : A general xml request error
    //* Returnvalue "ERROR SEARCH"  : The search value was not found
    //*
    //******************************************************************
    function SingleQuery(strSearch, strEntity, strAttributeX, strAttributeY)
    {
      var xml = "<?xml version='1.0' encoding='utf-8'?>"+
        "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
        GenerateAuthenticationHeader() +
        "  <soap:Body>"+
        "    <RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
        "      <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" +

                 //*** The Entity to search in ***
        "        <q1:EntityName>"+ strEntity +"</q1:EntityName>"+
        "        <q1:ColumnSet xsi:type='q1:ColumnSet'>"+
        "          <q1:Attributes>"+

                     //*** The attribute who contains the return value (output) ***
        "            <q1:Attribute>"+ strAttributeX +"</q1:Attribute>"+
        "          </q1:Attributes>"+
        "        </q1:ColumnSet>"+
        "        <q1:Distinct>false</q1:Distinct>"+
        "        <q1:Criteria>"+
        "          <q1:FilterOperator>And</q1:FilterOperator>"+
        "            <q1:Conditions>"+
        "              <q1:Condition>"+

                         //*** The attribute to search in (input) ***
        "                <q1:AttributeName>"+ strAttributeY +"</q1:AttributeName>"+
        "                <q1:Operator>Like</q1:Operator>"+
        "                  <q1:Values>"+

                             //*** The search string ***
        "                    <q1:Value xsi:type='xsd:string'>"+ strSearch +"</q1:Value>"+
        "                  </q1:Values>"+
        "              </q1:Condition>"+
        "            </q1:Conditions>"+
        "        </q1:Criteria>"+
        "      </query>"+
        "    </RetrieveMultiple>"+
        "  </soap:Body>"+
        "</soap:Envelope>";


        // Prepare the xmlHttpObject and send the request.
        var xHReq = new ActiveXObject("Msxml2.XMLHTTP");

        xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
        xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
        xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xHReq.setRequestHeader("Content-Length", xml.length);
        xHReq.send(xml);

        // Capture the result.
        var resultXml = xHReq.responseXML;
        var strReturnValue;

        // Check for errors.
        var errorCount = resultXml.selectNodes('//error').length;

        if (errorCount != 0)
        {
          return "ERROR GENERAL";
        }
        else   // No general errors found.
        {
          var resultXMLxml = resultXml.xml;

          // Check if a value is returned from the request.

          if (resultXMLxml.search("q1:"+ strAttributeX) == -1)
          {
            return "ERROR SEARCH";
          }
          else
          {
            // Parse and display the results.
            return strReturnValue = resultXml.selectSingleNode("//q1:"+ strAttributeX).nodeTypedValue;
          }
        }
     }

    //******************************************************************
    //* Query to return City (output) based on Postal Code (input)
    //******************************************************************
    var strZip = crmForm.all.address1_postalcode.DataValue;

    if (strZip != null)
    {
        // Post the query and get the result.
        var strQuery = SingleQuery(strZip, "new_postalcode", "new_streetname", "new_postalcode");

        switch (strQuery)
        {
            case "ERROR GENERAL" :
                alert("An unknown error occured! \n\nCheck if you have entered a valid zip code.");
                break;
            case "ERROR SEARCH" :
                alert("The City was not found! \n\nCheck if you have entered a valid zip code.");
                break;
            default :
                crmForm.all.address1_city.DataValue = strQuery;
                break;
        }
    }


    Henrik Jensen Instructor / Consultant MCP, MCAD, MCSD, MCTS, MCPD, MCITP, MBSS, MBSP, MCT www.crmblog.dk (Danish only)
    Friday, February 05, 2010 10:06 PM
  • Hi Varun,

    I think it will be good if you can save all the zip code (create one custom entity name "ZipCode" or something for this purpose)
    and then save all your zip code here. This way all the data will remain inside MS CRM. but you can still work if your ZipCode are in some external database or application.

    Regarding the Validation, you can do the validation in two different areas

    1) on the OnSave() event of the "Account" entity, in javascript.
    The dis-advatange to this is that these validation will not be applied if you create the account using the MS CRM sdk.

    2) or on the Pre-Create & Pre-Update event of the "Account" entity in the "Plugin".
    The advantage of the 2nd approach is that it will make sure your plugin is executed even if you create the accoutn from the MS CRM UI or from the MS CRM SDK. thus making a consistence business rule.

    i hope it helps.
    Thursday, February 04, 2010 5:54 AM
  • you can call the crm webservice to reterive all teh zip code from some custom entity.
    but hte validation part you have to do in javascript using a for loop on the return xml document from the above webservice.
    here is an example of how to call the crm webservice form the javascript. it may help u.
    http://blog.customereffective.com/blog/2008/02/calling-the-crm.html

    or you can use the stunware tool to generate the javascript for you.
    http://www.stunnware.com/crm2/topic.aspx?id=JSWebService2
    Thursday, February 04, 2010 6:30 AM

All replies

  • Hi Varun,

    I think it will be good if you can save all the zip code (create one custom entity name "ZipCode" or something for this purpose)
    and then save all your zip code here. This way all the data will remain inside MS CRM. but you can still work if your ZipCode are in some external database or application.

    Regarding the Validation, you can do the validation in two different areas

    1) on the OnSave() event of the "Account" entity, in javascript.
    The dis-advatange to this is that these validation will not be applied if you create the account using the MS CRM sdk.

    2) or on the Pre-Create & Pre-Update event of the "Account" entity in the "Plugin".
    The advantage of the 2nd approach is that it will make sure your plugin is executed even if you create the accoutn from the MS CRM UI or from the MS CRM SDK. thus making a consistence business rule.

    i hope it helps.
    Thursday, February 04, 2010 5:54 AM
  • Is there any wat to do it in the javascript while entering the form

    i mean form the UI, if the user enters the zip code can we validate it on blur
    i understand that there is onchange eventfor the attribute 
    but i want to know how to use that in this context
    Thursday, February 04, 2010 5:58 AM
  • yes the onchange event do exits for every field and its trigger after you chagne the text of the field and leave the focus() and you can write the code in teh onchange() event.
    you can either hard code all the zip code in javascript array and then match against this array whether the zip code is valid or not.

    or You can write a custom webservice(or if data is in ms crm use ms crm webservice) that can reterive all the zip code and then match the zip code against this return list from the webservice.
    Thursday, February 04, 2010 6:01 AM
  • do u have any code snippet for using the ms crm webservice for validating
    Thursday, February 04, 2010 6:08 AM
  • you can call the crm webservice to reterive all teh zip code from some custom entity.
    but hte validation part you have to do in javascript using a for loop on the return xml document from the above webservice.
    here is an example of how to call the crm webservice form the javascript. it may help u.
    http://blog.customereffective.com/blog/2008/02/calling-the-crm.html

    or you can use the stunware tool to generate the javascript for you.
    http://www.stunnware.com/crm2/topic.aspx?id=JSWebService2
    Thursday, February 04, 2010 6:30 AM
  • Hi Varun,

    You can also write a plugin on precreate to validate.
    Mahain
    Thursday, February 04, 2010 7:37 AM
  • yes it is true

    but we cannot alert the user that he is entering a wrong zip code

    plugin will execute only when we click on save button right

    but i want even before that
    Thursday, February 04, 2010 7:45 AM
  • Hi,

    You can through error and show message to user
    Check this post
    http://mscrm4ever.blogspot.com/2009/01/crm-40-creating-interactive-plug-ins.html

    Yes plugin will execute only when you will click on save button,if you want validation before this then you have to use JS.


    Mahain
    Thursday, February 04, 2010 9:35 AM
  • Hi,
    my suggestion is:

    1. Create a Custom entity that holds all zip codes
    2. On the Account, Zip code field OnChange event Fetch the Custom entity to lookup the zip code

    The are allot of samples on how to Fetch. Below i have a sample i often use to return a City based on a zip code.

    //******************************************************************
    //* Function that returns a single value
    //*
    //* PARAMETERS
    //* #1 (strSearch)       : VALUE to search for
    //* #2 (strEntity)         : ENTITY to search within
    //* #3 (strAttributeX)   : ATTRIBUTE to return the value from
    //* #4 (strAttributeX)   : ATTRIBUTE to search within
    //*
    //*
    //* RETURNVALUES
    //* Returnvalue "ERROR GENERAL" : A general xml request error
    //* Returnvalue "ERROR SEARCH"  : The search value was not found
    //*
    //******************************************************************
    function SingleQuery(strSearch, strEntity, strAttributeX, strAttributeY)
    {
      var xml = "<?xml version='1.0' encoding='utf-8'?>"+
        "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
        GenerateAuthenticationHeader() +
        "  <soap:Body>"+
        "    <RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
        "      <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" +

                 //*** The Entity to search in ***
        "        <q1:EntityName>"+ strEntity +"</q1:EntityName>"+
        "        <q1:ColumnSet xsi:type='q1:ColumnSet'>"+
        "          <q1:Attributes>"+

                     //*** The attribute who contains the return value (output) ***
        "            <q1:Attribute>"+ strAttributeX +"</q1:Attribute>"+
        "          </q1:Attributes>"+
        "        </q1:ColumnSet>"+
        "        <q1:Distinct>false</q1:Distinct>"+
        "        <q1:Criteria>"+
        "          <q1:FilterOperator>And</q1:FilterOperator>"+
        "            <q1:Conditions>"+
        "              <q1:Condition>"+

                         //*** The attribute to search in (input) ***
        "                <q1:AttributeName>"+ strAttributeY +"</q1:AttributeName>"+
        "                <q1:Operator>Like</q1:Operator>"+
        "                  <q1:Values>"+

                             //*** The search string ***
        "                    <q1:Value xsi:type='xsd:string'>"+ strSearch +"</q1:Value>"+
        "                  </q1:Values>"+
        "              </q1:Condition>"+
        "            </q1:Conditions>"+
        "        </q1:Criteria>"+
        "      </query>"+
        "    </RetrieveMultiple>"+
        "  </soap:Body>"+
        "</soap:Envelope>";


        // Prepare the xmlHttpObject and send the request.
        var xHReq = new ActiveXObject("Msxml2.XMLHTTP");

        xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
        xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
        xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xHReq.setRequestHeader("Content-Length", xml.length);
        xHReq.send(xml);

        // Capture the result.
        var resultXml = xHReq.responseXML;
        var strReturnValue;

        // Check for errors.
        var errorCount = resultXml.selectNodes('//error').length;

        if (errorCount != 0)
        {
          return "ERROR GENERAL";
        }
        else   // No general errors found.
        {
          var resultXMLxml = resultXml.xml;

          // Check if a value is returned from the request.

          if (resultXMLxml.search("q1:"+ strAttributeX) == -1)
          {
            return "ERROR SEARCH";
          }
          else
          {
            // Parse and display the results.
            return strReturnValue = resultXml.selectSingleNode("//q1:"+ strAttributeX).nodeTypedValue;
          }
        }
     }

    //******************************************************************
    //* Query to return City (output) based on Postal Code (input)
    //******************************************************************
    var strZip = crmForm.all.address1_postalcode.DataValue;

    if (strZip != null)
    {
        // Post the query and get the result.
        var strQuery = SingleQuery(strZip, "new_postalcode", "new_streetname", "new_postalcode");

        switch (strQuery)
        {
            case "ERROR GENERAL" :
                alert("An unknown error occured! \n\nCheck if you have entered a valid zip code.");
                break;
            case "ERROR SEARCH" :
                alert("The City was not found! \n\nCheck if you have entered a valid zip code.");
                break;
            default :
                crmForm.all.address1_city.DataValue = strQuery;
                break;
        }
    }


    Henrik Jensen Instructor / Consultant MCP, MCAD, MCSD, MCTS, MCPD, MCITP, MBSS, MBSP, MCT www.crmblog.dk (Danish only)
    Friday, February 05, 2010 10:06 PM
  • Great sample, i will try it out ASAP
    Saturday, February 06, 2010 1:43 PM