locked
Retrieve data from related record RRS feed

  • Question

  • Hi,

    When adding a contact to a campaign response, I wan't to make it possible to automatically retrieve the e-mail address from this contact: and add this e-mail to a field in the campaign response. So i've been looking in to athread, and found a script. 

    The related entity is contact, the contact lookup field in campaign response is named "partner", and the e-mail address I need from contact is named "emailaddress1". The script i tried just gives me an error on page.

    Any help on this would be appreciated.

    function getDataFromRelatedEntity(sEntityname, sAttribute, sLookupValue)
    {
    sEntityname = "contact";
    sAttribute = "emailaddress1";
    sLookupValue =
    crmForm.all.partner.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;
    }

    Thursday, April 15, 2010 11:10 PM

Answers

  • The code in original thread are not actually 100% correct, try this: 

    function getEmailFromContactEntity()
    {
      sEntityname = "contact";
      sAttribute = "contactid";
      sLookupValue = crmForm.all.partner.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 = "";
    
      if (nodeList.length == 0) {
        alert("The contact cannot be found. "); // This should never happen, if the patner field has valid contactid.
        return null;
      }
    
      return nodeList[i].selectSingleNode("./q1:emailaddress1").text;
    }
    
    The function should return the contact's email address for you. 


    Daniel Cai | http://danielcai.blogspot.com
    • Marked as answer by Carl I Friday, April 16, 2010 8:05 AM
    Thursday, April 15, 2010 11:50 PM
  • Hi,

    Try to Change this line

    return nodeList[i].selectSingleNode("./q1:emailaddress1").text;

    to

    return nodeList[0].selectSingleNode("./q1:emailaddress1").text;

     


    Mahain
    • Marked as answer by Carl I Friday, April 16, 2010 8:05 AM
    Friday, April 16, 2010 7:49 AM
    Moderator

All replies

  • The code in original thread are not actually 100% correct, try this: 

    function getEmailFromContactEntity()
    {
      sEntityname = "contact";
      sAttribute = "contactid";
      sLookupValue = crmForm.all.partner.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 = "";
    
      if (nodeList.length == 0) {
        alert("The contact cannot be found. "); // This should never happen, if the patner field has valid contactid.
        return null;
      }
    
      return nodeList[i].selectSingleNode("./q1:emailaddress1").text;
    }
    
    The function should return the contact's email address for you. 


    Daniel Cai | http://danielcai.blogspot.com
    • Marked as answer by Carl I Friday, April 16, 2010 8:05 AM
    Thursday, April 15, 2010 11:50 PM
  • Thanks Daniel,

    I'm not getting error on page anymore, so it's moving the right direction. But now i'm getting an alert telling me [   Error: 'i' is undefined ]

    What could this be?

    Thanks

    Friday, April 16, 2010 7:43 AM
  • Hi,

    Try to Change this line

    return nodeList[i].selectSingleNode("./q1:emailaddress1").text;

    to

    return nodeList[0].selectSingleNode("./q1:emailaddress1").text;

     


    Mahain
    • Marked as answer by Carl I Friday, April 16, 2010 8:05 AM
    Friday, April 16, 2010 7:49 AM
    Moderator
  • Works like a charm :)

    Thanks both

    Friday, April 16, 2010 8:05 AM