Address Lookup Button on Form

on Tuesday, 16 April 2013


In this tutorial I'm going to show you how to place an address lookup button on the contact form.

First, create a new solution in your environment and add the 'Contact' entity to it from the default solution (you are going to edit it later).
Next, download the JQuery library from here :

http://jquery.com/download/

You'll need to add this to your solution in the form of a JavaScript web-resource.  To do so, create a new web-resource in the solution and give it an appropriate name, make it of type JavaScript, copy and paste the JavaScript from the downloaded JQuery file into the text editor and save it.

Next, create another new JavaScript web-resource file, and call it something like 'Address Lookup Library'.  Add the following JavaScript in the text editor and save it:

//***********************
//Contact Functions
//***********************
function contactsOnLoad() {
  addButton('address1_addresstypecode')
}

function addButton(attributename) {
    if (document.getElementById(attributename) != null) {
        var sFieldID = "field" + attributename;
        var elementID = document.getElementById(attributename + "_d");
        var div = document.createElement("div");
        div.style.width = "100%";
        div.style.textAlign = "left";
        div.style.display = "inline";
        elementID.appendChild(div, elementID);
        div.innerHTML = '';
        document.getElementById(attributename).style.width = "100%";
        document.getElementById(sFieldID).onclick = function () {CustomLookup(); };
    }
}

//***********************
//Contact Address Lookup Functions
//***********************
function CustomLookup() {
  //Get value of the selected customer account
  var lookupItem = Xrm.Page.getAttribute("parentcustomerid").getValue();
  
  if (lookupItem != null)
  {
    // Lookup Customer info
    //On-line
    //var url = '/_controls/lookup/lookupsingle.aspx?class=BrowseCustomerAddress&objecttypes=1071&browse=1//&bindingcolumns=line1%2cpostalcode&parentType='
//        + lookupItem[0].type + '&parentId='
//        + lookupItem[0].id + '&ShowNewButton=1&ShowPropButton=1//&DefaultType=1071';

    //On-premise
    var url = '/' + Xrm.Page.context.getOrgUniqueName() +
      '/_controls/lookup/lookupsingle.aspx?class=BrowseCustomerAddress&objecttypes=1071&browse=1&bindingcolumns=line1%2cpostalcode&parentType='
        + lookupItem[0].type + '&parentId='
        + lookupItem[0].id + '&ShowNewButton=1&ShowPropButton=1&DefaultType=1071';


    // shows a modal window to select the addresses
    var selectedAddress = window.showModalDialog(url, null, 'dialogWidth:600px;dialogHeight:400px;resizable:yes');
    
    // Validate address info in not null before continuing
    if (selectedAddress != null)
    {
      var addressFields =  jQuery.parseJSON(selectedAddress);
      // we have the id of the address - now use the json to get the values in an array.
      if (addressFields.items != null)
{
 var sLookupValue = addressFields.items[0].id;
      var xml = "" + "" + "" + decodeURI(GenerateAuthenticationHeader()) + " " + " " + " " +
                         " customeraddress" +
     " " +
     " " +
     " city" +
     " country" +
     " fax" +
     " line1" +
     " line2" +
     " line3" +
     " name" +
     " postalcode" +
     " primarycontactname" +
     " stateorprovince" +
     " telephone1" +
     " " +
     " " +
     " " +
     " customeraddressid" +
     " " +
     " " +
     " " + sLookupValue + "" +
     " " +
     " " +
     " " +
     " " +
     " " + "";

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

      var resultXML = xmlHttpRequest.responseXml;

      //alert(xmlHttpRequest.responseText);
      var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
      oXmlDoc.async = false;
      oXmlDoc.loadXML(resultXML.xml);

      var businessEntities = oXmlDoc.getElementsByTagName('BusinessEntity');
                    
      for (i=0; i< 1; i++)
      {
        if (businessEntities[i].selectSingleNode('./q1:name') != null)
        {
          // Address Name Set
          Xrm.Page.getControl("address1_name").setDisabled(false);
          Xrm.Page.getAttribute("address1_name").setValue(businessEntities[i].selectSingleNode('./q1:name').text);
          Xrm.Page.getAttribute("address1_name").setSubmitMode("always");
          //Xrm.Page.getControl("address1_name").setDisabled(true);

          if (businessEntities[i].selectSingleNode('./q1:city') != null) {
            // Address City set
            Xrm.Page.getControl("address1_city").setDisabled(false);
            Xrm.Page.getAttribute("address1_city").setValue(businessEntities[i].selectSingleNode('./q1:city').text);
            Xrm.Page.getAttribute("address1_city").setSubmitMode("always");
            //Xrm.Page.getControl("address1_city").setDisabled(true);
          }
          else {
            // Address City set
            Xrm.Page.getControl("address1_city").setDisabled(false);
            Xrm.Page.getAttribute("address1_city").setValue(null);
            Xrm.Page.getAttribute("address1_city").setSubmitMode("always");
           // Xrm.Page.getControl("address1_city").setDisabled(true);
          }

          if (businessEntities[i].selectSingleNode('./q1:country') != null){
            // Country set
            Xrm.Page.getControl("address1_country").setDisabled(false);
            Xrm.Page.getAttribute("address1_country").setValue(businessEntities[i].selectSingleNode('./q1:country').text);
            Xrm.Page.getAttribute("address1_country").setSubmitMode("always");
            //Xrm.Page.getControl("address1_country").setDisabled(true);
          }
          else {
            // Country set
            Xrm.Page.getControl("address1_country").setDisabled(false);
            Xrm.Page.getAttribute("address1_country").setValue(null);
            Xrm.Page.getAttribute("address1_country").setSubmitMode("always");
            //Xrm.Page.getControl("address1_country").setDisabled(true);
          }               
          if (businessEntities[i].selectSingleNode('./q1:line1') != null){
            // Address Line 1 set
            Xrm.Page.getControl("address1_line1").setDisabled(false);
            Xrm.Page.getAttribute("address1_line1").setValue(businessEntities[i].selectSingleNode('./q1:line1').text);
            Xrm.Page.getAttribute("address1_line1").setSubmitMode("always");
            //Xrm.Page.getControl("address1_line1").setDisabled(true);
          }
          else {
            // Address Line 1 set
            Xrm.Page.getControl("address1_line1").setDisabled(false);
            Xrm.Page.getAttribute("address1_line1").setValue(null);
            Xrm.Page.getAttribute("address1_line1").setSubmitMode("always");
            //Xrm.Page.getControl("address1_line1").setDisabled(true);
          }

          if (businessEntities[i].selectSingleNode('./q1:line2') != null){
           // Address Line 2 set
           Xrm.Page.getControl("address1_line2").setDisabled(false);
           Xrm.Page.getAttribute("address1_line2").setValue(businessEntities[i].selectSingleNode('./q1:line2').text);
           Xrm.Page.getAttribute("address1_line2").setSubmitMode("always");
           //Xrm.Page.getControl("address1_line2").setDisabled(true);
         }
          else {
           // Address Line 2 set
           Xrm.Page.getControl("address1_line2").setDisabled(false);
           Xrm.Page.getAttribute("address1_line2").setValue(null);
           Xrm.Page.getAttribute("address1_line2").setSubmitMode("always");
           //Xrm.Page.getControl("address1_line2").setDisabled(true);
          }

         if (businessEntities[i].selectSingleNode('./q1:line3') != null){
           // Address Line 3 set
           Xrm.Page.getControl("address1_line3").setDisabled(false);
           Xrm.Page.getAttribute("address1_line3").setValue(businessEntities[i].selectSingleNode('./q1:line3').text);
           Xrm.Page.getAttribute("address1_line3").setSubmitMode("always");
           //Xrm.Page.getControl("address1_line3").setDisabled(true);
         }
          else {
           // Address Line 3 set
           Xrm.Page.getControl("address1_line3").setDisabled(false);
           Xrm.Page.getAttribute("address1_line3").setValue(null);
           Xrm.Page.getAttribute("address1_line3").setSubmitMode("always");
           //Xrm.Page.getControl("address1_line3").setDisabled(true);
          }

         if (businessEntities[i].selectSingleNode('./q1:postalcode') != null){
           //Address ZipCode set
           Xrm.Page.getControl("address1_postalcode").setDisabled(false);
           Xrm.Page.getAttribute("address1_postalcode").setValue(businessEntities[i].selectSingleNode('./q1:postalcode').text);
           Xrm.Page.getAttribute("address1_postalcode").setSubmitMode("always");
           //Xrm.Page.getControl("address1_postalcode").setDisabled(true);
         }
          else {
           //Address ZipCode set
           Xrm.Page.getControl("address1_postalcode").setDisabled(false);
           Xrm.Page.getAttribute("address1_postalcode").setValue(null);
           Xrm.Page.getAttribute("address1_postalcode").setSubmitMode("always");
           //Xrm.Page.getControl("address1_postalcode").setDisabled(true);
          }

         if (businessEntities[i].selectSingleNode('./q1:stateorprovince')!= null){
           //Address State set
           Xrm.Page.getControl("address1_stateorprovince").setDisabled(false);
           Xrm.Page.getAttribute("address1_stateorprovince").setValue(businessEntities[i].selectSingleNode('./q1:stateorprovince').text);
           Xrm.Page.getAttribute("address1_stateorprovince").setSubmitMode("always");
           //Xrm.Page.getControl("address1_stateorprovince").setDisabled(true);
         }
          else {
           //Address State set
           Xrm.Page.getControl("address1_stateorprovince").setDisabled(false);
           Xrm.Page.getAttribute("address1_stateorprovince").setValue(null);
           Xrm.Page.getAttribute("address1_stateorprovince").setSubmitMode("always");
          // Xrm.Page.getControl("address1_stateorprovince").setDisabled(true);
          }

}
     
       }// end if entity not empty
     }// end for i++
   }//end if no address selected
 }else {
     alert("No Parent Customer Selected.");
   }//  // end of if no customer seelcted
}// end of new address selected function

*Note: At the top of the function CustomLookup you'll notice there is a commented out section:

    //On-line
    //var url = '/_controls/lookup/lookupsingle.aspx?class=BrowseCustomerAddress&objecttypes=1071&browse=1//&bindingcolumns=line1%2cpostalcode&parentType='
//        + lookupItem[0].type + '&parentId='
//        + lookupItem[0].id + '&ShowNewButton=1&ShowPropButton=1//&DefaultType=1071';

Uncomment out this section and comment out the section:

    //On-premise
    var url = '/' + Xrm.Page.context.getOrgUniqueName() +
      '/_controls/lookup/lookupsingle.aspx?class=BrowseCustomerAddress&objecttypes=1071&browse=1&bindingcolumns=line1%2cpostalcode&parentType='
        + lookupItem[0].type + '&parentId='
        + lookupItem[0].id + '&ShowNewButton=1&ShowPropButton=1&DefaultType=1071';

If you would like to deploy this solution to a CRM On-line environment.

Finally, save and publish the solution and create a new contact.  There you have it, your address lookup button!  Simply click it and it will pull through one of the addresses related to the parent customer of the contact record.

0 comments:

Post a Comment