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
}



Christian Dollerup
December 16, 2011
Use the CRMRestKit found on Codeplex (http://crmrestkit.codeplex.com/). That way to can do the same with only 1/10 the lines of code.
Bill Thompson
May 31, 2013
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
Phil
June 11, 2013
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