title: CRM 2011: Getting entity with Javascript
body: <p>I am working on some CRM 2011 Online customisations and I need to get an entity using javascript.</p>
  
  <p>The entity I need will be based on the ID value of another field (a Contact entity) - this Contact ID I can get fine.</p>
  
  <p>The entity I want is a custom entity. There may be multiple matches based on the Contact ID so I just want to get the first one in the list (order not important)</p>
  
  <p>So far I have looked into a few ways to do this...</p>
  
  <ul>
  <li><p>OData - I couldn't find enough examples on this as to what query expressions I can create, also I don't know if/how to make this work for custom entities</p></li>
  <li><p>FetchXML - I can create a nice FetchXML query using the built-in "advanced find" too and would be happy to call this from javascript if anyone can help? I found one promising answer <a href="https://stackoverflow.com/questions/5876964/how-to-execute-fetchxml-in-crm-2011-using-a-crm-2011-webservice-and-javascript">here</a> but I could not see how the "results" return data was being set (Service.Fetch function)</p></li>
  <li><p>SOAP Request - First thing I tried is a similar method as I could have done in CRM 4 but this does not seem to work. Although the request executes, my result data just seems to be empty. This is all I have code for so if any one can spot a problem with the code below then that would be great.</p></li>
  </ul>
  
  <p><strong>EDIT</strong>: I have spotted some redundant query data (I had removed link opening tags but left closing tags) - since removing this I now get XML result data... however, the where clause does not seem to apply (just get list of all entities)</p>
  
  <pre><code>    var xml = "&lt;?xml version='1.0' encoding='utf-8'?&gt;" + 
      "&lt;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\"&gt;" + 
      GenerateAuthenticationHeader() +
      "&lt;soap:Body&gt;" + 
      "&lt;RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\"&gt;" + 
      "&lt;query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\"&gt;" + 
      "&lt;q1:EntityName&gt;new_vehicle&lt;/q1:EntityName&gt;" + 
      "&lt;q1:ColumnSet xsi:type='q1:ColumnSet'&gt;" + 
      "&lt;q1:Attributes&gt;" + 
      "&lt;q1:Attribute&gt;new_vehicleid&lt;/q1:Attribute&gt;" +
      "&lt;q1:Attribute&gt;new_primarydriver&lt;/q1:Attribute&gt;" +
      "&lt;q1:Attribute&gt;statuscode&lt;/q1:Attribute&gt;" +
      "&lt;q1:Attribute&gt;new_registration&lt;/q1:Attribute&gt;" +
      "&lt;/q1:Attributes&gt;" + 
      "&lt;/q1:ColumnSet&gt;" + 
      "&lt;q1:Distinct&gt;false&lt;/q1:Distinct&gt;" + 
  
      "&lt;q1:Conditions&gt;" + 
  
                       "&lt;q1:Condition&gt;" +
                       "&lt;q1:AttributeName&gt;new_primarydriver&lt;/q1:AttributeName&gt;" +
      "&lt;q1:Operator&gt;Equal&lt;/q1:Operator&gt;" +
      "&lt;q1:Values&gt;" +
      "&lt;q1:Value xmlns:q2='http://microsoft.com/wsdl/types/' xsi:type='q2:guid'&gt;" +
      customerID +
      "&lt;/q1:Value&gt;&lt;/q1:Values&gt;&lt;/q1:Condition&gt;" + 
  
      "&lt;/q1:Conditions&gt;" +
  
      "&lt;/query&gt;&lt;/RetrieveMultiple&gt;" +
      "&lt;/soap:Body&gt;&lt;/soap:Envelope&gt;";
  
  
      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);
  
      var result = xmlHttpRequest.responseXML.xml;
      var doc = new ActiveXObject("MSXML2.DOMDocument");
      doc.async = false;
      doc.loadXML(result);
  
      var id = doc.selectSingleNode("//new_vehicleid");
      var registration = doc.selectSingleNode("//new_registration");
  
      if(id == null)
         return null;
  
      var vehicle = new Array();
                       value[0] = new Object();
                       value[0].id = id;
                       value[0].name = registration;
                       value[0].entityType = "new_vehicle";
  
      return vehicle;
  </code></pre>
  
  <p>Sorry about the big code post but hopefully somebody who has a better understanding can help</p>