locked
select lookup value based on another lookup value in crm4.0 RRS feed

  • Question

  • Hi,

    In Case entity i have two lookup fields, Lookup1 is (Contact) and Lookup 2 is Compnay(Technically account), Now If I select contact then company value should automatically display in the Lookup2. Because when I create contact company is mandatory.

    Someone help me, how to achive this via JavaScript.

    Thanks in advance


    bhuma reddy

    Thursday, October 17, 2013 5:43 AM

Answers

  • Hi,

    first get the contact record.

    After you can retrieve values of a lookup using:

    var value = new Array();
    value[0] = new Object();
    value[0].id = idValue;
    value[0].name = textValue;
    value[0].typename = typeValue;

    Webservice to retrieve account:

    // Prepare variables to retrieve the contacts.
    var searchKey = <The account name>;
    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>"+
    "<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
    "<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'"+
    " xsi:type='q1:QueryExpression'>"+
    "<q1:EntityName>new_websitecreation</q1:EntityName>"+
    "<q1:ColumnSet xsi:type='q1:ColumnSet'>"+
    "<q1:Attributes>"+
    "<q1:Attribute>new_websitecreationid</q1:Attribute>"+
    "</q1:Attributes>"+
    "</q1:ColumnSet>"+
    "<q1:Distinct>false</q1:Distinct>"+
    "<q1:Criteria>"+
    "<q1:FilterOperator>And</q1:FilterOperator>"+
    "<q1:Conditions>"+
    "<q1:Condition>"+
    "<q1:AttributeName>new_name</q1:AttributeName>"+
    "<q1:Operator>Like</q1:Operator>"+
    "<q1:Values>"+
    "<q1:Value xsi:type='xsd:string'>"+searchKey+"</q1:Value>"+
    "</q1:Values>"+
    "</q1:Condition>"+
    "</q1:Conditions>"+
    "</q1:Criteria>"+
    "</query>"+
    "</RetrieveMultiple>"+
    "</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/RetrieveMultiple");
    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);
    }
    // Parse and display the results.
    else
    {
    var results = resultXml.getElementsByTagName('BusinessEntity');
    var msg = "";
    if (results.length == 0)
    {
      msg
    = "No records were found in "+searchKey+".";
      alert
    (msg);
     
    return;
    }
    else
    {
      msg
    = "Records found in "+searchCity+":\r";
      msg
    += "\WebSiteCreation Id\r";
      msg
    +="--------------------------------------------------------------------------------\r";
       
    for (i=0;i < results.length;i++)
       
    {
        
    var idValue = results[i].selectSingleNode('./q1:new_websitecreationid').nodeTypedValue;
         msg
    += idValue +"\t\r";
       
    }
        alert
    (msg);
    }
    }

    fill lookup with retrieved data:

    var lookupData = new Array();
                var lookupItem = new Object();
                lookupItem.typename = 'account';
                lookupItem.name = _Name;
                lookupItem.id = ID;
                lookupData[0] = lookupItem;
                crmForm.all.lookupfield.DataValue = lookupData;

    I have copied this out of other blocks, so there is no connection between the sample codes.

    Good Luck!

    Karan

    Thursday, October 17, 2013 7:24 AM

All replies

  • Hi,

    you have to pick out the contacts GUID, after that, you need a webservice to retrieve accounts GUID.

    Next you can fill the lookup.

    Do you need sample code?

    Karan

    • Proposed as answer by SravaniRN459 Thursday, October 17, 2013 7:07 AM
    Thursday, October 17, 2013 6:36 AM
  • Hi Karan,

    I appriciate if u can give the sample code.

    Thanks a lot.


    bhuma reddy

    Thursday, October 17, 2013 6:58 AM
  • Hi,

    first get the contact record.

    After you can retrieve values of a lookup using:

    var value = new Array();
    value[0] = new Object();
    value[0].id = idValue;
    value[0].name = textValue;
    value[0].typename = typeValue;

    Webservice to retrieve account:

    // Prepare variables to retrieve the contacts.
    var searchKey = <The account name>;
    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>"+
    "<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
    "<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'"+
    " xsi:type='q1:QueryExpression'>"+
    "<q1:EntityName>new_websitecreation</q1:EntityName>"+
    "<q1:ColumnSet xsi:type='q1:ColumnSet'>"+
    "<q1:Attributes>"+
    "<q1:Attribute>new_websitecreationid</q1:Attribute>"+
    "</q1:Attributes>"+
    "</q1:ColumnSet>"+
    "<q1:Distinct>false</q1:Distinct>"+
    "<q1:Criteria>"+
    "<q1:FilterOperator>And</q1:FilterOperator>"+
    "<q1:Conditions>"+
    "<q1:Condition>"+
    "<q1:AttributeName>new_name</q1:AttributeName>"+
    "<q1:Operator>Like</q1:Operator>"+
    "<q1:Values>"+
    "<q1:Value xsi:type='xsd:string'>"+searchKey+"</q1:Value>"+
    "</q1:Values>"+
    "</q1:Condition>"+
    "</q1:Conditions>"+
    "</q1:Criteria>"+
    "</query>"+
    "</RetrieveMultiple>"+
    "</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/RetrieveMultiple");
    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);
    }
    // Parse and display the results.
    else
    {
    var results = resultXml.getElementsByTagName('BusinessEntity');
    var msg = "";
    if (results.length == 0)
    {
      msg
    = "No records were found in "+searchKey+".";
      alert
    (msg);
     
    return;
    }
    else
    {
      msg
    = "Records found in "+searchCity+":\r";
      msg
    += "\WebSiteCreation Id\r";
      msg
    +="--------------------------------------------------------------------------------\r";
       
    for (i=0;i < results.length;i++)
       
    {
        
    var idValue = results[i].selectSingleNode('./q1:new_websitecreationid').nodeTypedValue;
         msg
    += idValue +"\t\r";
       
    }
        alert
    (msg);
    }
    }

    fill lookup with retrieved data:

    var lookupData = new Array();
                var lookupItem = new Object();
                lookupItem.typename = 'account';
                lookupItem.name = _Name;
                lookupItem.id = ID;
                lookupData[0] = lookupItem;
                crmForm.all.lookupfield.DataValue = lookupData;

    I have copied this out of other blocks, so there is no connection between the sample codes.

    Good Luck!

    Karan

    Thursday, October 17, 2013 7:24 AM
  • Hi Karan,

    Thank you very much for your quick reply, I will try and let you know how the things.


    bhuma reddy

    Thursday, October 17, 2013 7:34 AM