locked
Retreive Contact Lookup text value instead of GUID RRS feed

  • Question

  • I currently have a company field on the case form. I also have a contact lookup on the form, so when I pick a contact; I want to populate account name from the contact entity, but the column links back to a specfic account from account entity.

    I am trying to use fetch xml to populate account name in the company field when the user chooses a contact but I get a GUID instead of the text name. Is there anyway to retrieve text instead of GUID.

    Fetch xml is working but displaying a GUID number in field.

     

    Any thoughts???


    Jose Fleitas
    Friday, October 28, 2011 4:06 PM

Answers

  • Hello Hollodor and Julio,

    I used this simpler way.

    Creating an array which searchs parentcusomerid base on contact name.

     

    if (crmForm.all.customerid.DataValue != null) {
    var parentCustomerID = document.getElementById("customerid");
    var lookupItems = parentCustomerID.items[0].values;

    crmForm.mcaslt_companyname.value = lookupItems[1].value;


    }

    And this work perfectly.

    Jose


    Jose Fleitas
    • Marked as answer by JoseFleitas Wednesday, November 2, 2011 3:07 PM
    • Edited by JoseFleitas Thursday, November 17, 2011 10:37 PM
    Wednesday, November 2, 2011 3:06 PM

All replies

  • Try this fetch XML

    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
    "  <entity name='contact'>"+
    "    <attribute name='parentcustomerid' />"+
    "    <attribute name='contactid' />"+
    "    <order attribute='fullname' descending='false' />"+
    "    <filter type='and'>"+
    "      <condition attribute='contactid' operator='eq'  uitype='contact' value='"+<CONTACT ID>+"' />"+
    "    </filter>"+
    "    <link-entity name='account' from='accountid' to='parentcustomerid' visible='false' link-type='outer' alias='a_dc9b80f8c78146d89fd6a3b610836975'>"+
    "      <attribute name='name' />"+
    "    </link-entity>"+
    "    <link-entity name='contact' from='contactid' to='parentcustomerid' visible='false' link-type='outer' alias='a_df70b5872164433aa551e577fcb1c644'>"+
    "      <attribute name='fullname' />"+
    "    </link-entity>"+
    "  </entity>"+
    "</fetch>"

     

    Get data from "name" column if you intend to get the account name (if parentcustomer is an account).
    Get data from "fullname" column if you intend to get the name (if parentcustomer is a contact).

     


    Halldór Jóhannsson
    Friday, October 28, 2011 5:50 PM
  • Hello Halldor,

    I am still confuse how do you display full name from Contact lookup in Company Name field on the case form. Also, where do I in bed this script (on Change case form).

    Thanks,

    Jose

     


    Jose Fleitas
    Friday, October 28, 2011 8:40 PM
  • Halldor,

    After looking at your code, the end result should be getting the parentcustomer name to display in the company field in the case form. I think your end result is getting the full name or account name. Currently, I cant get the parentcustomer value to display in company field.

     

    Jose


    Jose Fleitas
    Friday, October 28, 2011 9:35 PM
  • If I understand you correct then what you want is when you select a customer on the case form and incase said customer is a contact you would like to get the parentcustomer name from the contact record you just selected to go into your company field on the case form, am I correct here?

     

    If that is the case then this script here should help you get said result.

    Insert these 3 function into your script for the case form and create a event for the customer field to fire the customer_OnChange function on change.

     

    This script will get from the parentcustomer field the name when a contact is selected on the case form, if an account is selected it will get the accountname, you can modify this to work with whatever you are actually doing if this is not the exact function you are going for.

     

    function customer_OnChange()
    {
     //Customer field name
     var customerAttribute = Xrm.Page.getAttribute("customerid").getValue();
     //Field that should be update with the account/contact name.
     var updateTextAttribute = Xrm.Page.getAttribute("description");//<-- DESCRIPTION NEEDS TO BE REPLACED WITH YOUR CUSTOM COMPANY FIELD NAME//

    if (customerAttribute != null)
    {
     // If there is data in the field, show it in a series of alerts
     if (customerAttribute[0] != null)
     {
       //Check the type of the customer item
       if(customerAttribute[0].typename == "contact")
       {
        var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
        "  <entity name='contact'>"+
        "    <attribute name='parentcustomerid' />"+
        "    <attribute name='contactid' />"+
        "    <order attribute='fullname' descending='false' />"+
        "    <filter type='and'>"+
        "      <condition attribute='contactid' operator='eq'  uitype='contact' value='" + customerAttribute[0].id + "' />"+
        "    </filter>"+
        "    <link-entity name='account' from='accountid' to='parentcustomerid' visible='false' link-type='outer' alias='accountname'>"+
        "      <attribute name='name' />"+
        "    </link-entity>"+
        "    <link-entity name='contact' from='contactid' to='parentcustomerid' visible='false' link-type='outer' alias='contactname'>"+
        "      <attribute name='fullname' />"+
        "    </link-entity>"+
        "  </entity>"+
        "</fetch>";
       
        //Query the CRM
        var oXmlDoc = sendRequest(fetchXml);
     
        if(oXmlDoc.getElementsByTagName("accountname.name")[0] != null && oXmlDoc.getElementsByTagName("accountname.name")[0].childNodes[0] != null && oXmlDoc.getElementsByTagName("accountname.name")[0].childNodes[0].nodeValue != null)
        {
         updateTextAttribute.setValue(oXmlDoc.getElementsByTagName("accountname.name")[0].childNodes[0].nodeValue.toString());
        }
        else if (oXmlDoc.getElementsByTagName("contactname.fullname")[0] != null && oXmlDoc.getElementsByTagName("contactname.fullname")[0].childNodes[0] != null && oXmlDoc.getElementsByTagName("contactname.fullname")[0].childNodes[0].nodeValue != null)
        {
         updateTextAttribute.setValue(oXmlDoc.getElementsByTagName("contactname.fullname")[0].childNodes[0].nodeValue.toString());
        }
       }//if(customerAttribute[0].typename == "contact") END
       else if (customerAttribute[0].typename == "account")
       {
        var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
        "  <entity name='account'>"+
        "    <attribute name='accountid' />"+
        "    <order attribute='name' descending='false' />"+
        "    <filter type='and'>"+
        "      <condition attribute='accountid' operator='eq'  uitype='account' value='" + customerAttribute[0].id + "' />"+
        "    </filter>"+
        "  </entity>"+
        "</fetch>";
       
        //Query the CRM
        var oXmlDoc = sendRequest(fetchXml);
     
        if(oXmlDoc.getElementsByTagName("name")[0] != null && oXmlDoc.getElementsByTagName("name")[0].childNodes[0] != null && oXmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue != null)
        {
         updateTextAttribute.setValue(oXmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue.toString());
        }
       }//else if (customerAttribute[0].typename == "account") END
      }//if (customerAttribute[0] != null) END
     }//if (customerAttribute != null) END
    }//function customer_OnChange() END

    //Function that handles querying the crm and returning a xml document
    function sendRequest(xmlFetchQuery)
    {

    //Create the XML send request
    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:Header>" +
    "    <CrmAuthenticationToken xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
    "      <AuthenticationType xmlns=\"0http://schemas.microsoft.com/crm/2007/CoreTypes\">0</AuthenticationType>" +
    "      <OrganizationName xmlns=\""+ORG_UNIQUE_NAME+"http://schemas.microsoft.com/crm/2007/CoreTypes\">"+ORG_UNIQUE_NAME+"</OrganizationName>" +
    "      <CallerId xmlns=\"00000000-0000-0000-0000-000000000000http://schemas.microsoft.com/crm/2007/CoreTypes\">00000000-0000-0000-0000-000000000000</CallerId>" +
    "    </CrmAuthenticationToken>" +
    "  </soap:Header>" +
    "  <soap:Body>" +
    "  <Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
    "<fetchXml>" +
    FetchEncode(xmlFetchQuery) +
    "</fetchXml>" +
    "</Fetch>"+
    "  </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/Fetch");
     xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
     xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
     xmlHttpRequest.send(xml);

     //Get response
     var resultXml = xmlHttpRequest.responseXML;

     // Capture the result and UnEncode it.
     var resultSet = new String();
     resultSet = resultXml.text;
     resultSet.replace('&lt;', '<');
     resultSet.replace('&gt;', '>');

     // Create an XML document that you can parse.
     var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
     oXmlDoc.async = false;

     // Load the XML document that has the UnEncoded results.
     oXmlDoc.loadXML(resultSet);

     //Return the oXmlDocument
     return oXmlDoc;
    }

    function FetchEncode( strInput ) //_HtmlEncode
    {
     var c;
     var HtmlEncode = '';
     if(strInput == null)
     {
     return null;
     }
     if (strInput == '')
     {
     return '';
     }
     for(var cnt = 0; cnt < strInput.length; cnt++)
     {
     c = strInput.charCodeAt(cnt);
     if (( ( c > 96 ) && ( c < 123 ) ) ||
     ( ( c > 64 ) && ( c < 91 ) ) ||
     ( c == 32 ) ||
     ( ( c > 47 ) && ( c < 58 ) ) ||
     ( c == 46 ) ||
     ( c == 44 ) ||
     ( c == 45 ) ||
     ( c == 95 ))
     {
      HtmlEncode = HtmlEncode + String.fromCharCode(c);
     }
     else
     {
      HtmlEncode = HtmlEncode + '&#' + c + ';';
     }
     }
     
     return HtmlEncode;
    }


    Halldór Jóhannsson
    Friday, October 28, 2011 9:52 PM
  • Hello Halldor,

    I look at your code. Look like this code pertains to using crm 2011. I currently using crm 4.0. I am still not familar with 2011, so some of the code is hard to understand.

    Do you know of any reference to use?

    I appreciate all your help,

    Jose Fleitas


    Jose Fleitas
    Monday, October 31, 2011 1:29 PM
  • Greetings JoseFleitas, sadly I do not have an installation of crm 4.0 here to test the script, we have moved to 2011 completely, however it should mostly be the same (Switch out Xrm.Page.getAttribute(attributename) with crmForm.all.attributename, which I have done already in the following script), also I used the GenerateAuthenticationHeader() function instead of using XML string text.

    Try this script and work your way from there, sadly also I do not remember if using the <fetchxml> works in crm 4.0, you  might have to do a <RetrieveMultiple> with <Query>, will include an example at the bottom (hoever if you need to go that way then most of the script you have from me here is something you would have to mostly rewrite).

     

    function customer_OnChange()
     {
      //Customer field name
      var customerAttribute = crmForm.all.customerid.DataValue;//<-- customerid Might be the wrong name for this field in CRM 4.0, but this is the only spot you should need to change it if it is //
      //Field that should be update with the account/contact name.
      var updateTextAttribute = crmForm.all.description;//<-- DESCRIPTION NEEDS TO BE REPLACED WITH YOUR CUSTOM COMPANY FIELD NAME//
     
    if (customerAttribute != null)
     {
      // If there is data in the field, show it in a series of alerts
      if (customerAttribute[0] != null)
     {
        //Check the type of the customer item
        if(customerAttribute[0].typename == "contact")
        {
         var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
         "  <entity name='contact'>"+
         "    <attribute name='parentcustomerid' />"+
         "    <attribute name='contactid' />"+
         "    <order attribute='fullname' descending='false' />"+
         "    <filter type='and'>"+
         "      <condition attribute='contactid' operator='eq'  uitype='contact' value='" + customerAttribute[0].id + "' />"+
         "    </filter>"+
         "    <link-entity name='account' from='accountid' to='parentcustomerid' visible='false' link-type='outer' alias='accountname'>"+
         "      <attribute name='name' />"+
         "    </link-entity>"+
         "    <link-entity name='contact' from='contactid' to='parentcustomerid' visible='false' link-type='outer' alias='contactname'>"+
         "      <attribute name='fullname' />"+
         "    </link-entity>"+
         "  </entity>"+
         "</fetch>";
        
        //Query the CRM
         var oXmlDoc = sendRequest(fetchXml);
     
         if(oXmlDoc.getElementsByTagName("accountname.name")[0] != null && oXmlDoc.getElementsByTagName("accountname.name")[0].childNodes[0] != null && oXmlDoc.getElementsByTagName("accountname.name")[0].childNodes[0].nodeValue != null)
         {
          updateTextAttribute.setValue(oXmlDoc.getElementsByTagName("accountname.name")[0].childNodes[0].nodeValue.toString());
         }
         else if (oXmlDoc.getElementsByTagName("contactname.fullname")[0] != null && oXmlDoc.getElementsByTagName("contactname.fullname")[0].childNodes[0] != null && oXmlDoc.getElementsByTagName("contactname.fullname")[0].childNodes[0].nodeValue != null)
         {
          updateTextAttribute.setValue(oXmlDoc.getElementsByTagName("contactname.fullname")[0].childNodes[0].nodeValue.toString());
         }
        }//if(customerAttribute[0].typename == "contact") END
        else if (customerAttribute[0].typename == "account")
        {
         var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
         "  <entity name='account'>"+
         "    <attribute name='accountid' />"+
         "    <order attribute='name' descending='false' />"+
         "    <filter type='and'>"+
         "      <condition attribute='accountid' operator='eq'  uitype='account' value='" + customerAttribute[0].id + "' />"+
         "    </filter>"+
         "  </entity>"+
         "</fetch>";
        
        //Query the CRM
         var oXmlDoc = sendRequest(fetchXml);
     
         if(oXmlDoc.getElementsByTagName("name")[0] != null && oXmlDoc.getElementsByTagName("name")[0].childNodes[0] != null && oXmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue != null)
         {
          updateTextAttribute.DataValue = (oXmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue.toString());
         }
        }//else if (customerAttribute[0].typename == "account") END
       }//if (customerAttribute[0] != null) END
      }//if (customerAttribute != null) END
     }//function customer_OnChange() END
     
     
    //Function that handles querying the crm and returning a xml document
     function sendRequest(xmlFetchQuery)
     {
     
    //Create the XML send request
     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>" +
    "  <Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
     "<fetchXml>" +
     FetchEncode(xmlFetchQuery) +
     "</fetchXml>" +
     "</Fetch>"+
    "  </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/Fetch");
      xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
      xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
      xmlHttpRequest.send(xml);
     
     //Get response
      var resultXml = xmlHttpRequest.responseXML;
     
     // Capture the result and UnEncode it.
      var resultSet = new String();
      resultSet = resultXml.text;
      resultSet.replace('&lt;', '<');
      resultSet.replace('&gt;', '>');
     
     // Create an XML document that you can parse.
      var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
     oXmlDoc.async = false;
     
     // Load the XML document that has the UnEncoded results.
      oXmlDoc.loadXML(resultSet);
     
     //Return the oXmlDocument
      return oXmlDoc;
     }
     
    function FetchEncode( strInput ) //_HtmlEncode
     {
      var c;
      var HtmlEncode = '';
      if(strInput == null)
      {
      return null;
      }
      if (strInput == '')
      {
      return '';
      }
      for(var cnt = 0; cnt < strInput.length; cnt++)
      {
      c = strInput.charCodeAt(cnt);
      if (( ( c > 96 ) && ( c < 123 ) ) ||
      ( ( c > 64 ) && ( c < 91 ) ) ||
      ( c == 32 ) ||
      ( ( c > 47 ) && ( c < 58 ) ) ||
      ( c == 46 ) ||
      ( c == 44 ) ||
      ( c == 45 ) ||
      ( c == 95 ))
      {
       HtmlEncode = HtmlEncode + String.fromCharCode(c);
      }
      else
     {
       HtmlEncode = HtmlEncode + '&#' + c + ';';
      }
      }
     
      return HtmlEncode;
     }

     

     

    >>>>>>>>>>>>>>>>>>>>>>>>>>>> EXAMPLE FOR RETRIEVEMULTIPLE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

     

    // Define the SOAP XML to access Microsoft Dynamics CRM Web service.
    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>" +
    // Specify the RetrieveMultiple message.
    "<RetrieveMultiple xmlns="+
    "\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
    // Specify that this is a QueryByAttribute query.
    "<query xmlns:q1="+
    "\"http://schemas.microsoft.com/crm/2006/Query\" "+
    "xsi:type=\"q1:QueryByAttribute\">" +
    // Query the customeraddress entity.
    "<q1:EntityName>customeraddress</q1:EntityName>" +
    // Set the columns you want to return.
    "<q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
    "<q1:Attributes>" +
    "<q1:Attribute>customeraddressid</q1:Attribute>" +
    "<q1:Attribute>name</q1:Attribute>" +
    "<q1:Attribute>addresstypecode</q1:Attribute>" +
    "<q1:Attribute>line1</q1:Attribute>" +
    "<q1:Attribute>line2</q1:Attribute>" +
    "<q1:Attribute>line3</q1:Attribute>" +
    "<q1:Attribute>city</q1:Attribute>" +
    "<q1:Attribute>stateorprovince</q1:Attribute>" +
    "<q1:Attribute>postalcode</q1:Attribute>" +
    "<q1:Attribute>country</q1:Attribute>" +
    "</q1:Attributes>" +
    "</q1:ColumnSet>" +
    // Specify the attribute that you are querying on.
    "<q1:Attributes>" +
    "<q1:Attribute>parentid</q1:Attribute>" +
    "</q1:Attributes>" +
    // Set the value of the attribute using the customerid
    // value of the case record.
    "<q1:Values>" +
    "<q1:Value xsi:type=\"xsd:string\">"+
    customerid+
    "</q1:Value>" +
    "</q1:Values>" +
    "</query>" +
    "</RetrieveMultiple>" +
    "</soap:Body>" +
    "</soap:Envelope>";
    // Create an instance of an XMLHTTP object.
    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    // Configure the XMLHttp object for the
    // Microsoft CRM Web services.
    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
      );
    // Send the XMLHttp request.
    xmlHttpRequest.send(xml);
    // Capture the XMLHttp response in XML format.
    var resultXml = xmlHttpRequest.responseXML;


    Halldór Jóhannsson
    Monday, October 31, 2011 2:55 PM
  • Hi Jose,

    You have to keep in mind 2 things: How to retrieve the information from another entity and How to set the look up.

    For the first point try this:

    Note: "responsiblecontactid" is the contact field.

    //RETRIEVE
    //Prepare variables.

    var ContID= crmForm.all.responsiblecontactid.DataValue[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>"+
    "<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
    "<fetchXml>&lt;fetch mapping='logical'&gt;"+
    "&lt;entity name='parentcustomerid'&gt;"+
    "&lt;attribute name='conty'/&gt;"+
    "&lt;filter type='and'&gt;"+
    "&lt;condition attribute='contactid' operator='eq' value='" + ContID+ "'/&gt;"+
    "&lt;/filter&gt;"+
    "&lt;link-entity name='account' from='accountid' to='parentcustomerid' visible='false' link-type='outer' alias='aa'&gt;"+
    "&lt;attribute name='name' /&gt;"+
    "&lt;/link-entity&gt;"+
    "&lt;/entity&gt;"+
    "&lt;/fetch&gt;</fetchXml>"+
    "</Fetch>"+
    "</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/Fetch);
    xHReq.setRequestHeader(
    "Content-Type", "text/xml; charset=utf-8");
    xHReq.setRequestHeader(
    "Content-Length", xml.length);
    xHReq.send(xml);

    //Capture the result

    varresultXml = xHReq.responseXML;//Check for errors.
    var errorCount = resultXml.selectNodes('//error').length;
    if(errorCount != 0)
    {
    var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
    alert(msg);
    }

    //Process and display the results.

    else
    {
    //Capture the result and UnEncode it.
    var resultSet = newString();
    resultSet = resultXml.text;
    resultSet.replace(
    '&lt;','<');
    resultSet.replace(
    '&gt;','>');

    // Create an XML document that you can parse.
    var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    oXmlDoc.async =
    false;

    // Load the XML document that has the UnEncoded results.

    oXmlDoc.loadXML(resultSet);

    //Display the results.

     

    var results = oXmlDoc.getElementsByTagName('result');
    var AccId = "";
    var AccIdName = "";

    for(i=0;i < results.length;i++)
    {
    if(results[i].selectSingleNode('./parentcustomerid') != null)
    AccId = results[i].selectSingleNode(
    './parentcustomerid').nodeTypedValue;
    }

    if(results[i].selectSingleNode('./aa.name') != null)
    AccIdName = results[i].selectSingleNode(
    './aa.name').nodeTypedValue;
    }
    }

      

    For the second point try this:

     

    //SET THE LOOKUP
    if(crmForm.all.customerid.DataValue==null){
    var lookup=newArray();
    var lookupitem = newObject();
    lookupitem.id=AccId
    ;
    lookupitem.typename=
    'account';
    lookupitem.name=AccIdName
    ;
    lookup[0]=lookupitem;
    crmForm.all.customerid.DataValue=lookup;
    }

     

    I hope that this can help you.


    Julio
    Monday, October 31, 2011 3:25 PM
  • Halldor,

    Thanks for all your help! I will try you script and implement it, and soon as I get it up and working.

    I appreciate your help!!!

     

     


    Jose Fleitas
    Monday, October 31, 2011 3:52 PM
  • Hello Halldor,

    I still cant get your script to work. I am getting a error message.  Here is the full script that I sort of Modified. I am getting on error in this section of the script:

    //Query the CRM
    var oXmlDoc = sendRequest(fetchXml);

    if(oXmlDoc.getElementsByTagName("accountname.name")[0] != null && oXmlDoc.getElementsByTagName("accountname.name")[0].childNodes[0] != null && oXmlDoc.getElementsByTagName("accountname.name")[0].childNodes[0].nodeValue != null)
    {
    updateTextAttribute.setValue(oXmlDoc.getElementsByTagName("accountname.name")[0].childNodes[0].nodeValue.toString());
    }
    else if (oXmlDoc.getElementsByTagName("contactname.fullname")[0] != null && oXmlDoc.getElementsByTagName("contactname.fullname")[0].childNodes[0] != null && oXmlDoc.getElementsByTagName("contactname.fullname")[0].childNodes[0].nodeValue != null)
    {
    updateTextAttribute.setValue(oXmlDoc.getElementsByTagName("contactname.fullname")[0].childNodes[0].nodeValue.toString());
    }
    }//if(customerAttribute[0].typename == "contact") END
    else if (customerAttribute[0].typename == "account")
    {
    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
    " <entity name='account'>"+
    " <attribute name='accountid' />"+
    " <order attribute='name' descending='false' />"+
    " <filter type='and'>"+
    " <condition attribute='accountid' operator='eq' uitype='account' value='" + customerAttribute[0].id + "' />"+
    " </filter>"+
    " </entity>"+
    "</fetch>";

    The above section is where I have having difficulties. The onchange event doesnt understand updateTextAttribute.setValue or setValue. Is this setValue CRM 2011 related.

     

     

     //Customer field name 
       var customerAttribute = crmForm.all.customerid.DataValue;

       //Field that should be update with the account/contact name.
       var updateTextAttribute = crmForm.all.mcaslt_companyname.DataValue;
     
     if (customerAttribute != null)
      {
       // If there is data in the field, show it in a series of alerts
       if (customerAttribute[0] != null)
     {
         //Check the type of the customer item
         if(customerAttribute[0].typename == "contact")
         {
          var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
          "  <entity name='contact'>"+
          "    <attribute name='parentcustomerid' />"+
          "    <attribute name='contactid' />"+
          "    <order attribute='fullname' descending='false' />"+
          "    <filter type='and'>"+
          "      <condition attribute='contactid' operator='eq'  uitype='contact' value='" + customerAttribute[0].id + "' />"+
          "    </filter>"+
          "    <link-entity name='account' from='accountid' to='parentcustomerid' visible='false' link-type='outer' alias='accountname'>"+
          "      <attribute name='name' />"+
          "    </link-entity>"+
          "    <link-entity name='contact' from='contactid' to='parentcustomerid' visible='false' link-type='outer' alias='contactname'>"+
          "      <attribute name='fullname' />"+
          "    </link-entity>"+
          "  </entity>"+
          "</fetch>";
         
        //Query the CRM
          var oXmlDoc = sendRequest(fetchXml);
      
         if(oXmlDoc.getElementsByTagName("accountname.name")[0] != null && oXmlDoc.getElementsByTagName("accountname.name")[0].childNodes[0] != null && oXmlDoc.getElementsByTagName("accountname.name")[0].childNodes[0].nodeValue != null)
          {
           updateTextAttribute.setValue(oXmlDoc.getElementsByTagName("accountname.name")[0].childNodes[0].nodeValue.toString());
          }
          else if (oXmlDoc.getElementsByTagName("contactname.fullname")[0] != null && oXmlDoc.getElementsByTagName("contactname.fullname")[0].childNodes[0] != null && oXmlDoc.getElementsByTagName("contactname.fullname")[0].childNodes[0].nodeValue != null)
          {
           updateTextAttribute.setValue(oXmlDoc.getElementsByTagName("contactname.fullname")[0].childNodes[0].nodeValue.toString());
          }
         }//if(customerAttribute[0].typename == "contact") END
         else if (customerAttribute[0].typename == "account")
         {
          var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
          "  <entity name='account'>"+
          "    <attribute name='accountid' />"+
          "    <order attribute='name' descending='false' />"+
          "    <filter type='and'>"+
          "      <condition attribute='accountid' operator='eq'  uitype='account' value='" + customerAttribute[0].id + "' />"+
          "    </filter>"+
          "  </entity>"+
          "</fetch>";
         
        //Query the CRM
          var oXmlDoc = sendRequest(fetchXml);
      
         if(oXmlDoc.getElementsByTagName("name")[0] != null && oXmlDoc.getElementsByTagName("name")[0].childNodes[0] != null && oXmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue != null)
          {
           updateTextAttribute.DataValue = (oXmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue.toString());
          }
         }//else if (customerAttribute[0].typename == "account") END
        }//if (customerAttribute[0] != null) END
       }//if (customerAttribute != null) END

     
     
     //Function that handles querying the crm and returning a xml document
      function sendRequest(xmlFetchQuery)
      {
     
     //Create the XML send request
      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>" +
    "  <Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
      "<fetchXml>" +
      FetchEncode(xmlFetchQuery) +
      "</fetchXml>" +
      "</Fetch>"+
    "  </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/Fetch");
       xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
       xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
       xmlHttpRequest.send(xml);
     
      //Get response
       var resultXml = xmlHttpRequest.responseXML;
     
      // Capture the result and UnEncode it.
       var resultSet = new String();
       resultSet = resultXml.text;
       resultSet.replace('&lt;', '<');
       resultSet.replace('&gt;', '>');
     
      // Create an XML document that you can parse.
       var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
     oXmlDoc.async = false;
     
      // Load the XML document that has the UnEncoded results.
       oXmlDoc.loadXML(resultSet);
     
      //Return the oXmlDocument
       return oXmlDoc;
      }
     
     function FetchEncode( strInput ) //_HtmlEncode
      {
       var c;
       var HtmlEncode = '';
       if(strInput == null)
       {
       return null;
       }
       if (strInput == '')
       {
       return '';
       }
       for(var cnt = 0; cnt < strInput.length; cnt++)
       {
       c = strInput.charCodeAt(cnt);
       if (( ( c > 96 ) && ( c < 123 ) ) ||
       ( ( c > 64 ) && ( c < 91 ) ) ||
       ( c == 32 ) ||
       ( ( c > 47 ) && ( c < 58 ) ) ||
       ( c == 46 ) ||
       ( c == 44 ) ||
       ( c == 45 ) ||
       ( c == 95 ))
       {
        HtmlEncode = HtmlEncode + String.fromCharCode(c);
       }
       else
     {
        HtmlEncode = HtmlEncode + '&#' + c + ';';
       }
       }
      
      return HtmlEncode;
      }


    Jose Fleitas
    Monday, October 31, 2011 7:30 PM
  • Hello Hallodor,

    Here is my short script I been using to populate the account name in the company field.

    if (crmForm.all.customerid.DataValue != null) {

        var FetchID = crmForm.all.customerid.DataValue[0].id;

        var authenticationHeader = GenerateAuthenticationHeader();

        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>contact</entityName>" +
        "<id>" + FetchID + "</id>" +
        "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
        "<q1:Attributes>";

        xml = xml + "<q1:Attribute>parentcustomerid</q1:Attribute>";


        xml = xml + "</q1:Attributes></columnSet>";

        xml = xml + "</Retrieve></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/Retrieve");

        xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

        xmlHttpRequest.setRequestHeader("Content-Length", xml.length);

        xmlHttpRequest.send(xml);

        resultXml= xmlHttpRequest.responseXML;
      
      
        if (resultXml.selectSingleNode('//q1:parentcustomerid') != null) {

            crmForm.mcaslt_companyname.DataValue = resultXml.selectSingleNode('//q1:parentcustomerid').nodeTypedValue
      
      

        }
      
     }

     

    The only issue I am having with this script is that it isn't showing the text value in the company field. Currently, when I click on the  contact and on change the company field will show the GUID number. This is my issue??? I need to display the text, not GUID.

    Still trying to find a solution.

     

     


    Jose Fleitas
    • Edited by JoseFleitas Monday, October 31, 2011 9:40 PM
    Monday, October 31, 2011 9:20 PM
  • Hello Hollodor and Julio,

    I used this simpler way.

    Creating an array which searchs parentcusomerid base on contact name.

     

    if (crmForm.all.customerid.DataValue != null) {
    var parentCustomerID = document.getElementById("customerid");
    var lookupItems = parentCustomerID.items[0].values;

    crmForm.mcaslt_companyname.value = lookupItems[1].value;


    }

    And this work perfectly.

    Jose


    Jose Fleitas
    • Marked as answer by JoseFleitas Wednesday, November 2, 2011 3:07 PM
    • Edited by JoseFleitas Thursday, November 17, 2011 10:37 PM
    Wednesday, November 2, 2011 3:06 PM