function GetDetails()
{
//put a new one of these for each field you want done
GetLookup('account', 'new_paaccount','new_paaccount','customerid'); //this does exactly what the origional script does
GetLookup('account', 'new_paaccount','new_pa2','customerid'); //this puts the same field into new_pa2 in the opportunity
GetLookup('EntityFrom', 'From_Field', 'To_Field', 'LinkField');
}
function GetLookup(fromEntity, fromField, toField, relationField) {
var formType= Xrm.Page.ui.getFormType();
if (formType ==1)
{
//20120809 Changes to allow multiple calls.
//arguments added to this function.
// fromEntity takes the place of our assumption that we were relating against an account.
// fromField tales the place of the var 'fieldname' from the origional script.
// toField takes the place of the var 'oppfieldname' from the origional script.
// relationfield takes the place of the assumption that the lookup for the account on the opportunity is named 'customerid'.
//useage (these will be case sensitive, so use exactly what is displayed in the customizations section of crm, normally lower case).
//GetLookup('account', 'new_paaccount','new_paaccount','customerid');
var EntityName, EntityId
var LookupType, LookupID, LookupName; //Note, these have been renamed
var resultXml;
//var fieldname = "new_paaccount"; //this is the name of the field on the accounts page - This is no longer used
//var oppfieldname = "new_paaccount"; //this is the name of the field on the opp page - This is no longer used
//This is the name of the field that contains the account id, change it if you need to.
LookupFieldObject = Xrm.Page.data.entity.attributes.get(relationField);
// If lookup field has value then the code will only run
if (LookupFieldObject.getValue() != null) {
//Fetch and place Entity Id (GUID) and Name (String) form lookup field into local variables
EntityId = LookupFieldObject.getValue()[0].id;
EntityName = LookupFieldObject.getValue()[0].entityType;
//new_patype is the name of the field that you want to get from the related account
//i would imagine you need to change this.
resultXml = RetrieveEntityById(EntityName, EntityId, fromField);
// In retrieved XML document check if it has primarycontactid lookup attribute
if (resultXml != null && resultXml.selectSingleNode('//q1:' +fromField) != null) {
// If XML document has primarycontactid lookup attribute then assign to local variable
LookupID = resultXml.selectSingleNode('//q1:' + fromField).nodeTypedValue;
// If XML document has primarycontactid lookup attribute then assign to local variable
LookupName = resultXml.selectSingleNode('//q1:' +fromField).getAttribute("name");
// If XML document has primarycontactid lookup attribute then assign to local variable
LookupType = resultXml.selectSingleNode('//q1:' + fromField).getAttribute("type") ;
Xrm.Page.getAttribute(toField).setValue( [{id: LookupID, name: LookupName, entityType: LookupType}]);
}
}
}
}
// Do not make any changes to this function
function RetrieveEntityById(prmEntityName, prmEntityId, prmEntityColumns) {
var resultXml, errorCount, msg, xmlHttpRequest, arrayEntityColumns, xmlEntityColumns;
arrayEntityColumns = prmEntityColumns.split(",");
for (var i = 0; i < arrayEntityColumns.length; i++) {
xmlEntityColumns += "<q1:Attribute>" + arrayEntityColumns[i] + "</q1:Attribute>";
}
var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();
//Prepare the SOAP message.
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'>" +
authenticationHeader +
"<soap:Body>" +
"<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<entityName>" + prmEntityName + "</entityName>" +
"<id>" + prmEntityId + "</id>" +
"<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
"<q1:Attributes>" +
xmlEntityColumns +
"</q1:Attributes>" +
"</columnSet>" +
"</Retrieve></soap:Body></soap:Envelope>";
//call function to create Soap Request to ms crm webservice
xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
resultXml = xmlHttpRequest.responseXML;
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0) {
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert("Error Message : " + msg);
}
else {
return resultXml;
}
}