Tuesday, September 18, 2012

CRM 2011 - javascript to retrieve data from lookup entity and map fields

function getcontactdetails() {
    var EntityName, EntityId,LookupFieldObject;
    var Name = "";      
    var resultXml;
    LookupFieldObject = Xrm.Page.data.entity.attributes.get('jmh_customer');
    if (LookupFieldObject.getValue() != null) {
        EntityId = LookupFieldObject.getValue()[0].id;
        EntityName = LookupFieldObject.getValue()[0].entityType;
        resultXml = RetrieveEntityById(EntityName, EntityId, 'jmh_title,fullname,address1_line1');
        if (resultXml != null && resultXml.selectSingleNode('//q1:jmh_title') != null) {
            Name = Name + resultXml.selectSingleNode('//q1:jmh_title').nodeTypedValue;
       }
       if (resultXml != null && resultXml.selectSingleNode('//q1:fullname') != null) {
           Name = Name +" "+ resultXml.selectSingleNode('//q1:fullname').nodeTypedValue;
       }
       if (resultXml != null && resultXml.selectSingleNode('//q1:address1_line1') != null) {
           Xrm.Page.data.entity.attributes.get('jmh_addressline1').setValue(resultXml.selectSingleNode('//q1:address1_line1').nodeTypedValue);
       }

}


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

Thanks to
Athul MT

Reference :
http://worldofdynamics.blogspot.in/2011/06/microsoft-dynamics-crm-2011-retrieve.html

Note:
to set lookup field
  if (resultXml != null && resultXml.selectSingleNode('//q1:defaultuomid') != null) {
         var lookupValue = new Array();
            lookupValue[0] = new Object();
            lookupValue[0].id = resultXml.selectSingleNode('//q1:defaultuomid').nodeTypedValue;
            lookupValue[0].name = resultXml.selectSingleNode('//q1:defaultuomid').getAttribute("name");
             lookupValue[0].entityType =  resultXml.selectSingleNode('//q1:
defaultuomid').getAttribute("type");

            Xrm.Page.data.entity.attributes.get('uomid').setValue(lookupValue);
        }

2 comments:

  1. Hi ,
    I am new to CRM and I have a question on how to auto populate text from lookup value.

    I went through many forum, and got solution for 2 entites, but i am looking for 4 entities mentioned below:



    In our CRM instance, there are 4 entities mentioned below:

    Case

    Location

    Finance Region Location

    Finance Region

    Case has N:1 Relationship with Location and Finance Region.

    Location has 1:N relationship with Finance Region Location.

    Finance Region Location has N:1 relationship with Finance Region.

    When user select one value in Location lookup in case form, Finance Region should be auto populate

    for example: if user enter asia as location, in finance region field(Read only text box) APAC should be autopopulate.



    Could you please let me know help to write javascript code to autopopulate Finance Region text box Please help me out
    Thanks in advance

    ReplyDelete