Java to set lookup value to related entity field...which is also a lookup

Answered Java to set lookup value to related entity field...which is also a lookup

  • Monday, May 14, 2012 12:14 PM
     
      Has Code

    Hi

    I am trying to use Java Script to populate a lookupvalue with the value of a related entity's lookup field.

    The Java Script is On Save of entity3.
    Entity3 is related to entity2.
    And entity2 is related to entity1.
    Entity3 also has a relationship to entity1...which would be the same entity as which entity2 has a relationship with.
    I am trying to set this relationship, between entity3 and entity1

    When entity3 is saved (with a relationship to entity2 in place), I want to set it's relationship with entity1 to equal entity2's relationship with entity1

    I hope that made sense...below my code...but I can't seem to get e3 to e1 set.

    //Prepare variables for a entity3 to retrieve.
    var lookupItem = new Array;
    
    // Get the lookup for the ‘’ attribute on entity2
    lookupItem = crmForm.all.new_entity2id.DataValue;
    
    var entity2id = lookupItem[0].id;
    
    var authenticationHeader = GenerateAuthenticationHeader();
    
    //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>new_entity2</entityName>"+ 
    "<id>"+entity2id+"</id>"+ 
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+ 
    "<q1:Attributes>"+ 
    "<q1:Attribute>new_somefield1</q1:Attribute>"+ 
    "<q1:Attribute>new_somefield2</q1:Attribute>"+ 
    "<q1:Attribute>new_entity3id</q1:Attribute>"+ 
    "</q1:Attributes>"+ 
    "</columnSet>"+ 
    "</Retrieve>"+ 
    "</soap:Body>"+ 
    "</soap:Envelope>";
    //Prepare the xmlHttpObject and send the request.
    var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
    xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
    xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xHReq.setRequestHeader("Content-Length", xml.length);
    xHReq.send(xml);
    //Capture the result.
    var resultXml = xHReq.responseXML;
    
    //Check for errors.
    var errorCount = resultXml.selectNodes('//error').length;
    if (errorCount != 0)
    {
    var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
    alert(msg);
    }
    //Display the retrieved value.
    else
    {
    
    var someField1 = parseFloat(resultXml.selectSingleNode("//q1:new_somefield1").nodeTypedValue);
    crmForm.all.new_myField1.DataValue = someField1;
    crmForm.all.new_arrearbalance.ForceSubmit = true;
    
    var someField2 = parseFloat(resultXml.selectSingleNode("//q1:new_somefield2").nodeTypedValue);
    crmForm.all.new_myField2.DataValue =someField2;
    crmForm.all.new_myField2.ForceSubmit = true;
    
    var entity3 = parseFloat(resultXml.selectSingleNode("//q1:new_entity3id").nodeTypedValue);
    crmForm.all.new_entity3id.DataValue = entity3;
    crmForm.all.new_entity3id.ForceSubmit = true;
    }
    

All Replies

  • Monday, May 14, 2012 12:53 PM
     
     Proposed Answer

    Hello,

    refer below links for more help

    https://crmdev.wordpress.com/2010/05/29/setting-a-crm-4-0-lookup-field-in-javascript/

    https://crmdev.wordpress.com/2011/01/26/setting-a-crm-4-0-lookup-field-in-javascript-part-deux/

    http://social.microsoft.com/Forums/is/crmdevelopment/thread/f6568274-6cf1-490d-9440-97c0e38390a0


    Thanks, Ankit Shah


    Inkey Solutions, India.
    Microsoft Certified Business Management Solutions Professionals
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html

  • Monday, May 14, 2012 1:03 PM
     
     Answered Has Code

    Hi,

    If I understand correctly...

    You have two lookups in your entity3 and want to populate entity1's lookup value automatically based on the entity2 lookup selected (with the value entity1 lookup that is available on entity2).

    If that's correct then you need to write logic in your entity2 lookup field OnChange event.

    1. retrieve the entity1 lookup name, id from entity2.

    2. then populate entity1 lookup into entity3.

    JS code to populate lookup value in CRM 4.0

    //Create an array to set as the DataValue for the lookup control.
    var lookupData = new Array();
    //Create an Object add to the array.
       var lookupItem= new Object();
    //Set the id, typename, and name properties to the object.
       lookupItem.id = '{1AAC1363-01A1-DB11-8432-0003FF9CE217}';
       lookupItem.typename = 'entity1';
       lookupItem.name = 'test record';
    // Add the object to the array.
       lookupData[0] = lookupItem;
    // Set the value of the lookup field to the value of the array.
       crmForm.all.entity1id.DataValue = lookupData;
    


    • Marked As Answer by Basquiat Monday, May 14, 2012 1:53 PM
    •