locked
How to get lookup value from a text field RRS feed

  • Question

  • Hi All;

    I need urgent help to get a value from a text field to a look field

    for eg text A displays value CA123

    Based on this text field i need to auto populate the lookup field (say Lookup B) as elligible

    Any help on this is much appreciated

    Thanks


    Pradnya07

    Wednesday, October 17, 2012 11:02 AM

Answers

  • Hi,

    Ok, now it's clear...

    You have to insert a javascript function on the onchange-event of "CAS Ward Code" - in this function you have to load the "eligible ward"-data depending on the value of "CAS Ward Code" and set the loaded data to the lookup.

    Here is the function to load the guid:

    etAttributeValueFromID = function(sEntityName, sGUID, sAttributeName, sID)  
    {  
        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\">" +  
        GenerateAuthenticationHeader() +  
        "  <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>"+sEntityName+"</q1:EntityName>" +  
        "        <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +  
        "          <q1:Attributes>" +  
        "            <q1:Attribute>"+sAttributeName+"</q1:Attribute>" +  
        "          </q1:Attributes>" +  
        "        </q1:ColumnSet>" +  
        "        <q1:Distinct>false</q1:Distinct>" +  
        "        <q1:PageInfo>" +  
        "          <q1:PageNumber>1</q1:PageNumber>" +  
        "          <q1:Count>1</q1:Count>" +  
        "        </q1:PageInfo>" +  
        "        <q1:Criteria>" +  
        "          <q1:FilterOperator>And</q1:FilterOperator>" +  
        "          <q1:Conditions>" +  
        "            <q1:Condition>" +  
        "              <q1:AttributeName>"+sID+"</q1:AttributeName>" +  
        "              <q1:Operator>Equal</q1:Operator>" +  
        "              <q1:Values>" +  
        "                <q1:Value xsi:type=\"xsd:string\">"+sGUID+"</q1:Value>" +  
        "              </q1:Values>" +  
        "            </q1:Condition>" +  
        "          </q1:Conditions>" +  
        "        </q1:Criteria>" +  
        "      </query>" +  
        "    </RetrieveMultiple>" +  
        "  </soap:Body>" +  
        "</soap:Envelope>" +  
        "";  
      
        var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");  
      
        xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);  
        xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");  
        xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");  
        xmlHttpRequest.setRequestHeader("Content-Length", xml.length);  
        xmlHttpRequest.send(xml);  
      
        // retrieve response and find attribute value  
        var result = xmlHttpRequest.responseXML.selectSingleNode("//q1:" + sAttributeName);  
        if (result == null)  
            return "";  
        else  
            return result.text;  
    }
    
    //********************************************************
    
    GetAttributeValueFromID("<name of eligible ward entity", "<value of CAS Ward Code", "<name of guid attribute of eligible ward", "<name of attribute on eligible ward which holds cas ward code>");

    After loading the guid you only have to set the lookup value of eligible ward like this: http://crmbusiness.wordpress.com/2011/02/18/crm-2011-how-to-set-up-a-lookup-using-javascript/

    br,

    Andreas


    Andreas Buchinger
    Microsoft Dynamics Certified Technology Specialist
    MCPD: SharePoint Developer 2010

    • Marked as answer by Simran08 Monday, October 22, 2012 8:08 AM
    Wednesday, October 17, 2012 1:24 PM
  • Hi,

    You distorted the parameters:

    WardCode=function()
    {
    
    var id = GetAttributeValueFromID("new_erdfwardlookup", "00BNFK","new_caswardcode","new_erdfwardlookupid");
    var name = GetAttributeValueFromID("new_erdfwardlookup","00BNFK", "new_caswardcode","new_name");
    
    Xrm.Page.getAttribute("new_erdfeligibleward").setValue( [{id: id, name: name, entityType: "new_erdfwardlookup"}]);
    }

    In addition why do you insert a hardcoded Wardcode ("OOBNFK") - this has to be dependent on your select before.

    I would urgent suggest you to contact a programmer or a consultant for your requirement - I'm sorry, but I cannot help you more than this in context of this forum.

    br,

    Andreas


    Andreas Buchinger
    Microsoft Dynamics Certified Technology Specialist
    MCPD: SharePoint Developer 2010

    • Proposed as answer by Andreas Buchinger Monday, October 22, 2012 8:04 AM
    • Marked as answer by Simran08 Monday, October 22, 2012 8:08 AM
    Monday, October 22, 2012 8:04 AM

All replies

  • Hi,

    We need much more information on this requirement: what crm version, what entity type is the lookup

    For info: If you type the text directly in the lookup field the crm form search for the equilant data automatically and offers you the found possibilities - this should solve your problem too!

    Best regards,

    Andreas


    Andreas Buchinger
    Microsoft Dynamics Certified Technology Specialist
    MCPD: SharePoint Developer 2010

    Wednesday, October 17, 2012 12:51 PM
  • Hi Andreas;

    Thanks for your reply

    As per the attached form

    When user enter the post code ...CAS ward code gets displayed  which runs fine

    As per the CAS ward code i need to display the  Eligible Ward? lookup

    Eligible ward look up contains field Eligible ward , CAS ward Code

    The data in Eligible ward is as follows

    Any help much appreciated

    Thanks


    Pradnya07

    Wednesday, October 17, 2012 1:05 PM
  • Hi,

    Ok, now it's clear...

    You have to insert a javascript function on the onchange-event of "CAS Ward Code" - in this function you have to load the "eligible ward"-data depending on the value of "CAS Ward Code" and set the loaded data to the lookup.

    Here is the function to load the guid:

    etAttributeValueFromID = function(sEntityName, sGUID, sAttributeName, sID)  
    {  
        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\">" +  
        GenerateAuthenticationHeader() +  
        "  <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>"+sEntityName+"</q1:EntityName>" +  
        "        <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +  
        "          <q1:Attributes>" +  
        "            <q1:Attribute>"+sAttributeName+"</q1:Attribute>" +  
        "          </q1:Attributes>" +  
        "        </q1:ColumnSet>" +  
        "        <q1:Distinct>false</q1:Distinct>" +  
        "        <q1:PageInfo>" +  
        "          <q1:PageNumber>1</q1:PageNumber>" +  
        "          <q1:Count>1</q1:Count>" +  
        "        </q1:PageInfo>" +  
        "        <q1:Criteria>" +  
        "          <q1:FilterOperator>And</q1:FilterOperator>" +  
        "          <q1:Conditions>" +  
        "            <q1:Condition>" +  
        "              <q1:AttributeName>"+sID+"</q1:AttributeName>" +  
        "              <q1:Operator>Equal</q1:Operator>" +  
        "              <q1:Values>" +  
        "                <q1:Value xsi:type=\"xsd:string\">"+sGUID+"</q1:Value>" +  
        "              </q1:Values>" +  
        "            </q1:Condition>" +  
        "          </q1:Conditions>" +  
        "        </q1:Criteria>" +  
        "      </query>" +  
        "    </RetrieveMultiple>" +  
        "  </soap:Body>" +  
        "</soap:Envelope>" +  
        "";  
      
        var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");  
      
        xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);  
        xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");  
        xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");  
        xmlHttpRequest.setRequestHeader("Content-Length", xml.length);  
        xmlHttpRequest.send(xml);  
      
        // retrieve response and find attribute value  
        var result = xmlHttpRequest.responseXML.selectSingleNode("//q1:" + sAttributeName);  
        if (result == null)  
            return "";  
        else  
            return result.text;  
    }
    
    //********************************************************
    
    GetAttributeValueFromID("<name of eligible ward entity", "<value of CAS Ward Code", "<name of guid attribute of eligible ward", "<name of attribute on eligible ward which holds cas ward code>");

    After loading the guid you only have to set the lookup value of eligible ward like this: http://crmbusiness.wordpress.com/2011/02/18/crm-2011-how-to-set-up-a-lookup-using-javascript/

    br,

    Andreas


    Andreas Buchinger
    Microsoft Dynamics Certified Technology Specialist
    MCPD: SharePoint Developer 2010

    • Marked as answer by Simran08 Monday, October 22, 2012 8:08 AM
    Wednesday, October 17, 2012 1:24 PM
  • Hi Andreas

    I am completely new to this i am still not getting this code

    i kept this function GetAttributeValueFromID = function(sEntityName, sGUID, sAttributeName, sID) 

    untouched

    enter values in

    GetAttributeValueFromID("<name of eligible ward entity", "<value of CAS Ward Code", "<name of guid attribute of eligible ward", "<name of attribute on eligible ward which holds cas ward )

    Am i doing right

    Pradnya



    Pradnya07

    Thursday, October 18, 2012 8:34 AM
  • Hi,

    Yes, you doing right...

    Copy the method in a webressource which is linked with the form - add the line to call the method in your onload-method of the form - set the values in the method call correct. You will get the guid from the method - take this guid and set the attributes to your lookup.

    br,

    Andreas


    Andreas Buchinger
    Microsoft Dynamics Certified Technology Specialist
    MCPD: SharePoint Developer 2010

    Thursday, October 18, 2012 8:39 AM
  • Hi I am sorry for the trouble but i think i am doing something terrible wrong

    below is the code

    Any help much appreciated

    alert(GetAttributeValueFromID("new_erdfwardlookup","new_hpwcaswardcode", "fd140aaf-4df4-11dd-bd17-0019b9312238",  "new_caswardcode"));

    new_erdfwardlookup is the entity name of eligible ward entity
    new_hpwcaswardcode is the value of CAS ward code in teh form
    "fd140aaf-4df4-11dd-bd17-0019b9312238" is the guid attribute of eligible ward entity (This is how i took it "https://tools/solution/edit.aspx?id=

    new_caswardcode is

    the field name in entity name of eligible ward entity


    Pradnya07


    %7bfd140aaf-4df4-11dd-bd17-0019b9312238%7d#")

    • Edited by Simran08 Thursday, October 18, 2012 10:22 AM
    Thursday, October 18, 2012 9:57 AM
  • Hi,

    Call method like this:

    var id = GetAttributeValueFromID("new_erdfwardlookup", "CA 231", "new_erdfwardlookupid", "new_hpwcaswardcode");
    var name = GetAttributeValueFromID("new_erdfwardlookup", "CA 231", "new_name", "new_hpwcaswardcode");
    
    Xrm.Page.getAttribute(<name of eligible ward attribute>).setValue( [{id: id, name: name, entityType: "new_erdfwardlookup"}]);

    br,

    Andreas


    Andreas Buchinger
    Microsoft Dynamics Certified Technology Specialist
    MCPD: SharePoint Developer 2010

    Thursday, October 18, 2012 1:22 PM
  • Hi Andreas,

    I am not sure you noted or not that Simran08 is using CRM 2011 and you are suggesting Old CRM service code.

    It's better to use REST or SOAP request according to CRM 2011 coding standard.

    Simran08 :- what we are suggesting -

    1. You can create filter lookup.

    2. You can retrieve the record based on text box and populate the lookup.

    http://msdn.microsoft.com/en-us/library/gg334427.aspx

    Thursday, October 18, 2012 1:45 PM
  • Hi _Vikram,

    No, I don't noticed this - you are right. Regarding the CRM 2011 coding standard the usage of REST or SOAP would be better - but although my code would work and would be less effort for Simran08 to use...

    br,

    Andreas


    Andreas Buchinger
    Microsoft Dynamics Certified Technology Specialist
    MCPD: SharePoint Developer 2010

    Thursday, October 18, 2012 1:55 PM
  • Hi

    My full code goes like this

    GetAttributeValueFromID = function(sEntityName, sGUID, sAttributeName, sID) 

        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\">" + 
        GenerateAuthenticationHeader() + 
        "  <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>"+sEntityName+"</q1:EntityName>" + 
        "        <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" + 
        "          <q1:Attributes>" + 
        "            <q1:Attribute>"+sAttributeName+"</q1:Attribute>" + 
        "          </q1:Attributes>" + 
        "        </q1:ColumnSet>" + 
        "        <q1:Distinct>false</q1:Distinct>" + 
        "        <q1:PageInfo>" + 
        "          <q1:PageNumber>1</q1:PageNumber>" + 
        "          <q1:Count>1</q1:Count>" + 
        "        </q1:PageInfo>" + 
        "        <q1:Criteria>" + 
        "          <q1:FilterOperator>And</q1:FilterOperator>" + 
        "          <q1:Conditions>" + 
        "            <q1:Condition>" + 
        "              <q1:AttributeName>"+sID+"</q1:AttributeName>" + 
        "              <q1:Operator>Equal</q1:Operator>" + 
        "              <q1:Values>" + 
        "                <q1:Value xsi:type=\"xsd:string\">"+sGUID+"</q1:Value>" + 
        "              </q1:Values>" + 
        "            </q1:Condition>" + 
        "          </q1:Conditions>" + 
        "        </q1:Criteria>" + 
        "      </query>" + 
        "    </RetrieveMultiple>" + 
        "  </soap:Body>" + 
        "</soap:Envelope>" + 
        ""; 
     
        var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
     
        xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false); 
        xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple"); 
        xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); 
        xmlHttpRequest.setRequestHeader("Content-Length", xml.length); 
        xmlHttpRequest.send(xml); 
     
        // retrieve response and find attribute value 
        var result = xmlHttpRequest.responseXML.selectSingleNode("//q1:" + sAttributeName); 
        if (result == null) 
            return ""; 
        else 
            return result.text;
    //GetAttributeValueFromID("new_erdfwardlookup","new_hpwcaswardcode", "A9A3ACD0-F3B4-4294-940D-A0B83C4A7787",  "new_caswardcode");

    }

    GetAttributeValueFromID("new_erdfwardlookup","new_hpwcaswardcode", "A9A3ACD0-F3B4-4294-940D-A0B83C4A7787",  "new_caswardcode");

    onchange event of of Eligible ward

    WardCode=function()
    {

    var id = GetAttributeValueFromID("new_erdfwardlookup", "A9A3ACD0-F3B4-4294-940D-A0B83C4A7787", "new_erdfwardlookupid", "new_hpwcaswardcode");
    var name = GetAttributeValueFromID("new_erdfwardlookup", "A9A3ACD0-F3B4-4294-940D-A0B83C4A7787", "new_name", "new_hpwcaswardcode");
    Xrm.Page.getAttribute("new_caswardcode").setValue( [{id: id, name: name, entityType: "new_erdfwardlookup"}]);
    }

    Am i doing wrong

    Any help much appreciated

    Thanks

     


    Pradnya07

    Friday, October 19, 2012 9:59 AM
  • Hi,

    First point: you don't have to call the function after definition

    Second point: In the call of the onchange event you have to replace the guid with the text of your CAS ward code - like I mentioned above.

    To clarify the call: The method searches the entity "new_erdfwardlookup" (first parameter) by filtering the data on the attribute "new_hpwcaswardcode" (fourth parameter) and the value "CA 231" (second parameter). The attribute "new_erdwardlookupid" (third parameter) will be returned after executing!

    br,

    Andreas


    Andreas Buchinger
    Microsoft Dynamics Certified Technology Specialist
    MCPD: SharePoint Developer 2010

    Friday, October 19, 2012 10:05 AM
  • Hi Andreas

    Still no luck

    i did as per your suggestion

    i wrote the belwo function on change event of Eligible ward

    WardCode=function()
    {

    var id = GetAttributeValueFromID("new_erdfwardlookup", "new_caswardcode","00BNFK", "new_erdfwardlookupid");
    var name = GetAttributeValueFromID("new_erdfwardlookup","new_caswardcode", "00BNFK","new_name");

    Xrm.Page.getAttribute("new_erdfeligibleward").setValue( [{id: id, name: name, entityType: "new_erdfwardlookup"}]);
    }

    Am i doing something wrong again

    Please do let me know

    Thanks a load

     


    Pradnya07

    Friday, October 19, 2012 12:21 PM
  • Hi,

    You distorted the parameters:

    WardCode=function()
    {
    
    var id = GetAttributeValueFromID("new_erdfwardlookup", "00BNFK","new_caswardcode","new_erdfwardlookupid");
    var name = GetAttributeValueFromID("new_erdfwardlookup","00BNFK", "new_caswardcode","new_name");
    
    Xrm.Page.getAttribute("new_erdfeligibleward").setValue( [{id: id, name: name, entityType: "new_erdfwardlookup"}]);
    }

    In addition why do you insert a hardcoded Wardcode ("OOBNFK") - this has to be dependent on your select before.

    I would urgent suggest you to contact a programmer or a consultant for your requirement - I'm sorry, but I cannot help you more than this in context of this forum.

    br,

    Andreas


    Andreas Buchinger
    Microsoft Dynamics Certified Technology Specialist
    MCPD: SharePoint Developer 2010

    • Proposed as answer by Andreas Buchinger Monday, October 22, 2012 8:04 AM
    • Marked as answer by Simran08 Monday, October 22, 2012 8:08 AM
    Monday, October 22, 2012 8:04 AM
  • Thank you so much

    Pradnya07

    Monday, October 22, 2012 8:09 AM