CRM 2011 – Javascript code to lookup parent customer addresses from contact form

I had written this code before but it is the property of my previous employer.  I could rewrite fairly easily because I have recently done the same thing in CRM 4 based on  code similar to this – http://www.stunnware.com/crm2/topic.aspx?id=JS26

but good news for me and other people out there JoshJWilliams has recently written the code and copied it to this forum post

so well done JoshJWilliams for perserving and solving the problem and thanks for copying the code up for the CRM community to use.

here is the code


function customerid_onchange(){
 // Get value of the selected customer account
 var lookupItem = Xrm.Page.getAttribute("parentcustomerid").getValue();

if (lookupItem != null)
 {
 //alert(lookupItem);

// Lookup Customer info
 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)
 {
 //alert(selectedAddress);
 var addressFields = selectedAddress.items;
 // we have the id of the address -now use the json to get the values in an array.
 sLookupValue = addressFields[0].id;
 //alert(sLookupValue);

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\">" +
 decodeURI(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:QueryByAttribute\">" +
 " <q1:EntityName>customeraddress</q1:EntityName>" +
 " <q1:ColumnSet xsi:type=\"q1:ColumnSet\" >" +
 " <q1:Attributes>" +
 " <q1:Attribute>city</q1:Attribute>" +
 " <q1:Attribute>country</q1:Attribute>" +
 " <q1:Attribute>fax</q1:Attribute>" +
 " <q1:Attribute>line1</q1:Attribute>" +
 " <q1:Attribute>line2</q1:Attribute>" +
 " <q1:Attribute>line3</q1:Attribute>" +
 " <q1:Attribute>name</q1:Attribute>" +
 " <q1:Attribute>postalcode</q1:Attribute>" +
 " <q1:Attribute>primarycontactname</q1:Attribute>" +
 " <q1:Attribute>stateorprovince</q1:Attribute>" +
 " <q1:Attribute>telephone1</q1:Attribute>" +
 " </q1:Attributes>" +
 " </q1:ColumnSet>" +
 " <q1:Attributes>" +
 " <q1:Attribute>customeraddressid</q1:Attribute>" +
 " </q1:Attributes>" +
 " <q1:Values>" +
 " <q1:Value xsi:type=\"xsd:string\">" + sLookupValue + "</q1:Value>" +
 " </q1:Values>" +
 " </query>" +
 " </RetrieveMultiple>" +
 " </soap:Body>" +
 " </soap:Envelope>" + "";

xml = xml.replace(/:/g, ":");
 xml = xml.replace(///g, "/");

//window.clipboardData.setData("text", xml);
 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');
 /*if (businessEntities[i].selectSingleNode('./q1:name') != null)
 {
 alert(businessEntities[0].selectSingleNode('./q1:name').nodeTypedValue);
 }
 alert(businessEntities[0].selectSingleNode('./q1:city').nodeTypedValue);
 alert(businessEntities[0].selectSingleNode('./q1:country').nodeTypedValue);
 alert(businessEntities[0].selectSingleNode('./q1:line1').nodeTypedValue);
 alert(businessEntities[0].selectSingleNode('./q1:postalcode').nodeTypedValue);
 alert(businessEntities[0].selectSingleNode('./q1:stateorprovince').nodeTypedValue);
 }
 }
 } */

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);
 }

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);
 }

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);
 }
 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);
 }

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);
 }

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);
 }

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);
 }

}// end if entity not empty
 }// end for i++

}// end if no address selected
 } // end of if no customer seelcted
 // end of new address selected function
 }

7 thoughts on “CRM 2011 – Javascript code to lookup parent customer addresses from contact form

  1. Bill Thompson May 31, 2013 / 12:59 pm

    Hi,

    This is a really useful solution, and one that a Client has requested today.
    Can you please provide steps of how to implement it.
    If I create the above code as a Web Resource and add to the contact Form, what else is required – how is it triggered on the Contact Form etc?

    Thanks

    Like

  2. Phil June 11, 2013 / 6:30 pm

    Do you know why I can’t get the value from the showModalDialog?, when I debug my code, the value I get from it is undefined :S

    Like

  3. Rick January 27, 2014 / 9:11 pm

    I get an undefined error on customerid_onchange. Checked all my syntax. I added the jquery and json2 libraries (don’t you need those since you are calling a json function?). Here’s what I did, I added the 2 libraries, I created a web resource ContactMain.js, added Josh’s code. I fire Josh’s code Onchange of the parentcustomerid lookup field on the contact form. I can only assume I have some null value that I’m not trapping for or the lookup is not pulling the right key.

    How is this implemented per Bill’s question above?

    Like

    • Hosk January 28, 2014 / 9:51 am

      press F12 and start debugging and then put a breakpoint in and step through the code to see where the error is occuring

      Like

  4. Phil January 28, 2014 / 5:07 pm

    The error was related with the context where I was executing my page, I mean, I was calling my page from a link outside of CRM, that was the reason why I was getting an undefined value. When I called my page directly from CRM, it worked fine. 🙂

    Like

  5. Rick January 28, 2014 / 10:38 pm

    I agree I should use the debugger but before I go down that path can you tell me what libraries I need (i.e. Jquery, Json2,) if any, and if I fire the code from the contact form or do I fire it from the parentcustomerid lookup field on a OnChange event? The setup of how this orks is not clear. And finally, will this work on CRM2011 Update RU15 or CRM 2013? Thanks.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s