Tuesday 18 December 2012

Set lookup field while calling a Entity/Activity from ribbon button in CRM 2011


Here the look-up field is set based on another look-up field in the target entity.

function setLookupfield() {
        if (Xrm.Page.data.entity.attributes.get('targetfieldName').getValue()==null) {
            var EntityName, EntityId, LookupFieldObject;
            var Name = "";
            var resultXml;
            LookupFieldObject = Xrm.Page.data.entity.attributes.get('regardingobjectid');
            if (LookupFieldObject.getValue() != null) {
                EntityId = LookupFieldObject.getValue()[0].id;
                EntityName = LookupFieldObject.getValue()[0].entityType;
                resultXml = RetrieveEntityById(EntityName, EntityId, 'customerid');
                if (resultXml != null && resultXml.selectSingleNode('//q1:customerid') != null) {
                    var lookupValue = new Array();
                    lookupValue[0] = new Object();                  
                    lookupValue[0].id = resultXml.selectSingleNode('//q1:customerid').nodeTypedValue;
                    lookupValue[0].name = resultXml.selectSingleNode('//q1:customerid').getAttribute("name");
                    lookupValue[0].entityType = 'account';

                    Xrm.Page.data.entity.attributes.get('targetfieldname').setValue(lookupValue);
                }
            }
        }
    }
//Do not make any changes
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;
    }
}

After adding the javascript to webresources you need to call setLookupfield() function on page on-load event on the target entity.

No comments:

Post a Comment