locked
retrieve values from a lookup field in ms crm 4.0 RRS feed

  • Question

  • Hi,

    In the Case form I have lookup field say lookup1 related to entity new_entity1.when user changes lookup1, another lookup field(lookup2- related to entity new_entity2) in case form should be automatically populated with a value.This value comes from a lookup field in new_entity for the record selected in lookup1.

    I used retrieve message in js to retrieve the guid of lookup2 for the record selected in lookup1.
    I used the foloowing code to poulate lookup2


    //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 = guidretrievedfromentity1;
       lookupItem.typename = 'entity2';

    // Add the object to the array.
       lookupData[0] = lookupItem;
    // Set the value of the lookup field to the value of the array.
       crmForm.all.lookup2.DataValue = lookupData;

    When i change the lookup1, the lookup2 is populated only with the lookup symbol but does not show the name.
    Any idea why this behaviour

    Regards
    Raji

    Monday, March 30, 2009 10:36 AM

Answers

  • I don't think so. You make a SOAP to get the corresponding GUID from another lookup.
    Only then are you able to find the corresponding name from that perticular record. I suggest you create a JS function to call your SOAP requests this way. Saves a lot of lines in code.

    Here is a function I wrote some time ago to get multiple values. It was intended to get multiple records as well, but since you only have one records (using the GUID, it should work the same).


    function getDataFromRelatedEntity(sEntityname, sAttribute, sLookupValue)
    {
    sEntityname = "entity1";
    sAttribute = "lookup2id";
    sLookupValue = crmForm.all.lookup1.DataValue[0].id

    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\">" +
    " <soap:Body>" +
    " <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\" xmlns=\"http://schemas.microsoft.com/crm/2006/WebServices\">" +
    " <q1:EntityName>" + sEntityname + "</q1:EntityName>" +
    " <q1:ColumnSet xsi:type=\"q1:AllColumns\" />" +
    " <q1:Distinct>false</q1:Distinct>" +
    " <q1:Criteria>" +
    " <q1:FilterOperator>And</q1:FilterOperator>" +
    " <q1:Conditions>" +
    " <q1:Condition>" +
    " <q1:AttributeName>" + sAttribute + "</q1:AttributeName>" +
    " <q1:Operator>Equal</q1:Operator>" +
    " <q1:Values>" +
    " <q1:Value xsi:type=\"xsd:string\">" + sLookupValue + "</q1:Value>" +
    " </q1:Values>" +
    " </q1:Condition>" +
    " </q1:Conditions>" +
    " </q1:Criteria>" +
    " </query>" +
    " </soap:Body>" +
    "</soap:Envelope>" + "";

    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    xmlHttpRequest.Open("POST", "/mscrmservices/2006/CrmService.asmx", false);
    xmlHttpRequest.setRequestHeader("SOAPAction",http://schemas.microsoft.com/crm/2006/WebServices/RetrieveMultiple);
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
    xmlHttpRequest.send(xml);

    var resultXML = xmlHttpRequest.responseXml;
    var nodeList = resultXML.selectNodes("//BusinessEntity") // all subelements are selected here
    var result = "";

    for(i = 0; i < nodeList.length; i++)
    {
    rsName
    = nodeList[i].selectSingleNode("./q1:name2").text;
    rsGuid = nodeList[i].selectSingleNode("./q1:lookup2id").text;
    result = "";
    }
    return result;
    }

    Monday, March 30, 2009 12:40 PM

All replies

  • You als have to set the .name part of the array. This is the same as in CRM 3.0.
    Example:

    lookupItem.id = guidretrievedfromentity1;
    lookupItem.typename = 'entity2';
    lookupItem.name = 'myname';

    lookupData[0] = lookupItem;
    crmForm.all.lookup2.DataValue = lookupData;

    Since you retrieve the GUID from another lookup, you can use the following to retrieve the name:

    lookupItem.name = crmForm.all.lookup.DataValue[0].name (use .id to get the GUID).

    Or use a SOAP retrieve script.
    • Proposed as answer by Daniel Middel Monday, March 30, 2009 11:15 AM
    Monday, March 30, 2009 11:12 AM
  • Hi Daniel,

    I am not sure how to get the name of lookup2.
    I cannot use the following line,because lookup2 is not in case form but in entity2 form
    lookupItem.name = crmForm.all.lookup.DataValue[0].name (use .id to get the GUID).

    Regards
    Raji
    Monday, March 30, 2009 11:25 AM
  • How did you retrieve the GUID then?
    I see this in your code: lookupItem.id = guidretrievedfromentity1;

    Monday, March 30, 2009 11:47 AM
  • Hi Daniel,

    I used the below line to get guid of entity1 selected in lookup1 found in case form
    guidretrievedfromentity1=crmForm.all.lookup1.DataValue[0].id//(in onchange of case form)

    Then I used
    SOAP retrieve script to get guid of entity2 from lookup2 found in entity1

    Now I want the name of lookup2 from the guid of entity2.Is it possible to get it without using SOAP retrieve script
    I cannot use the following line,because lookup2 has to be populated automatically
    lookupItem.name = crmForm.all.lookup.DataValue[0].name (use .id to get the GUID).


    The entity relationships are

    Case has 2 lookups
    lookup1(related to entity1)
    lookup2(related to entity2)---------->I want to poulate this field to the value from entity1 once the user changes lookup1

    Entity1 has one lookup

    lookup2(related to entity2)


    Monday, March 30, 2009 11:56 AM
  • I understand. You can use the same SOAP function to retrieve the name as well. Because you already have the GUID of the record.
    Search entity1 and find the name attribute where your entity1id equals guidretrievedfromentity1.

    guidretrievedfromentity1=crmForm.all.lookup1.DataValue[0].id//(in onchange of case form)
    lookupItem.name = yourSOAPfunction





    Monday, March 30, 2009 12:05 PM
  • Sorry I didnot get it.
    I will rephrase my question

    THis is my code in case onchange event,
    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>entity1</entityName>"+
    "<id>"+crmForm.all.lookup1.DataValue[0].id+"</id>"+
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+
    "<q1:Attributes>"+
    "<q1:Attribute>lookup2</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;
    var lookup2guid=resultXml.selectSingleNode("//q1:lookup2").nodeTypedValue;

    Now I have only the guid of entity2 from lookup2
    I think that i have to use another SOAP function retrieve to get the name of lookup2 corresponding to lookup2guid


     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>entity2</entityName>"+
    "<id>"+lookup2guid+"</id>"+
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+
    "<q1:Attributes>"+
    "<q1:Attribute>name</q1:Attribute>"+
    "</q1:Attributes>"+
    "</columnSet>"+
    "</Retrieve>"+
    "</soap:Body>"+
    "</soap:Envelope>";

    Now I can get the name from the below line.

    resultXml.selectSingleNode("//q1:name").nodeTypedValue

    My question is instead of using the above soap request is it possible to get the name of the lookup from the first soap request only?
    Hope my question is clear.


    Regards

    Raji
    Monday, March 30, 2009 12:21 PM
  • I don't think so. You make a SOAP to get the corresponding GUID from another lookup.
    Only then are you able to find the corresponding name from that perticular record. I suggest you create a JS function to call your SOAP requests this way. Saves a lot of lines in code.

    Here is a function I wrote some time ago to get multiple values. It was intended to get multiple records as well, but since you only have one records (using the GUID, it should work the same).


    function getDataFromRelatedEntity(sEntityname, sAttribute, sLookupValue)
    {
    sEntityname = "entity1";
    sAttribute = "lookup2id";
    sLookupValue = crmForm.all.lookup1.DataValue[0].id

    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\">" +
    " <soap:Body>" +
    " <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\" xmlns=\"http://schemas.microsoft.com/crm/2006/WebServices\">" +
    " <q1:EntityName>" + sEntityname + "</q1:EntityName>" +
    " <q1:ColumnSet xsi:type=\"q1:AllColumns\" />" +
    " <q1:Distinct>false</q1:Distinct>" +
    " <q1:Criteria>" +
    " <q1:FilterOperator>And</q1:FilterOperator>" +
    " <q1:Conditions>" +
    " <q1:Condition>" +
    " <q1:AttributeName>" + sAttribute + "</q1:AttributeName>" +
    " <q1:Operator>Equal</q1:Operator>" +
    " <q1:Values>" +
    " <q1:Value xsi:type=\"xsd:string\">" + sLookupValue + "</q1:Value>" +
    " </q1:Values>" +
    " </q1:Condition>" +
    " </q1:Conditions>" +
    " </q1:Criteria>" +
    " </query>" +
    " </soap:Body>" +
    "</soap:Envelope>" + "";

    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    xmlHttpRequest.Open("POST", "/mscrmservices/2006/CrmService.asmx", false);
    xmlHttpRequest.setRequestHeader("SOAPAction",http://schemas.microsoft.com/crm/2006/WebServices/RetrieveMultiple);
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
    xmlHttpRequest.send(xml);

    var resultXML = xmlHttpRequest.responseXml;
    var nodeList = resultXML.selectNodes("//BusinessEntity") // all subelements are selected here
    var result = "";

    for(i = 0; i < nodeList.length; i++)
    {
    rsName
    = nodeList[i].selectSingleNode("./q1:name2").text;
    rsGuid = nodeList[i].selectSingleNode("./q1:lookup2id").text;
    result = "";
    }
    return result;
    }

    Monday, March 30, 2009 12:40 PM
  • I've tested this. But having some difficulties. This is what i wan't to do: When adding a contact to a campaign response, i'd like to retrieve the e-mail address from the contact, and add this to the campaign response form.

    Name of my fields:

    Lookup for contact at campaign response: partner
    E-mail field on contact: emailaddress1

    So i put toghether this with your script.

    sEntityname = "contact";
    sAttribute = "emailaddress1";
    sLookupValue =
    crmForm.all.partner.DataValue[0].id

    I haven't changed anything else in the code. But when loading the campaign response form, it loads with an error on page status. what have I done wrong?


    Thursday, April 15, 2010 10:22 PM
  • Hi Carl, 

    It might be good idea to start a new thread. It's hard to read all the history. 

    Most likely the query is not valid. Better start a new thread and post your code. 

    Cheers,


    Daniel Cai | http://danielcai.blogspot.com
    Thursday, April 15, 2010 10:27 PM
  • Carl,

    One thing to be aware of is that the Customer attribute on the campaign response is NOT a standard lookup. It is a partylist that shows all activityparty records associated with the campaign response. If you are using the native Customer field on the campaign response form, then this code will not work for you. I really HATE dealing with the campaign response activity record and the Customer field! It's never as easy as you would like.

    Tuesday, July 13, 2010 6:08 PM