title: Dynamics CRM 2011 form jscript to retrieve lookup data
body: <p><strong>What are my mistakes, why do I get the "object expected" error, and, eventually, how does one debug jScript ?</strong>  </p>
  
  <p>I am new to Dynamics CRM and I would like to do a small customisation, which seem to require jScript. The instance (version 2011)  is used mostly to manage client support.  </p>
  
  <p>There are 2 custom entities with relationships:  FundLegalEntity --> SubFund </p>
  
  <p>The Case (Incident) form is linked to the FundLegalEntity and the SubFund.<br>
  When user enters a SubFund I would like to have the FundLegalEntity filled automatically (if   empty).<br>
  My question was: <em>how do I code that</em> ? </p>
  
  <p>With the help of <a href="http://mshelp.be/creating-a-calculated-field-in-dynamics-crm2011-356.htm" rel="nofollow">this great tutorial</a>, and <a href="http://crm2011odatatool.codeplex.com" rel="nofollow">the very usefull oData Tool</a>, and great help (below) from user @dub, here is my latest code:</p>
  
  <pre class="lang-js prettyprint-override"><code>function recalcParent()
  { 
      var lookupValue = Xrm.Page.getAttribute("new_subfundid").getValue();   
  
      var subFundId= lookupValue[0].id;
      // alert(subFundId);
  
      var request =  Xrm.Page.context.getServerUrl() + 
          "/xrmServices/2011/OrganizationData.svc/new_subfundSet?" + 
          "$select=new_LegalEntityId&amp;" + 
          "$filter=new_subfundId eq guid'"+ subFundId+ "'";
      // alert(request);
  
  $.ajax({
      type: "GET",
      contentType: "application/json; charset=utf-8",
      datatype: "json",
      url: request,
      async: false,
      beforeSend: 
          function (XMLHttpRequest) 
          { 
              XMLHttpRequest.setRequestHeader("Accept", "application/json"); 
          },
      success: 
          function (data, textStatus, XmlHttpRequest) 
          {
              var result = data.d.results[0];
              alert(result);
              var parentFundLookup = [{ entityType : "new_fund", id : result.LegalEntityId, name : result.FundLegalEntity}];                  
              // Set the value of the parent fund lookup 
          },
      error: 
          function (XmlHttpRequest, textStatus, errorThrown) 
          { 
              alert('Failed'); 
          }
      });       
  
  } 
  </code></pre>
  
  <p>I have no more errors, the first 2 alerts (commente out) are giving me correct results.
  THe 3rd alert displays "object Object", and the control I expect to update is not updated.<br>
  Any hint please ?  I suppose the last problem is in the <code>var parentFundLookup =</code> line...<br>
  I am a bit confused by all these different names.<br>
  Thanks !  </p>
  
  <hr>
  
  <h2>Edit:</h2>
  
  <p>It's nearly working now: when I modify the sub-fund in the Incident, the Legal Entity gets updated with the correct legal entity name, but the text box has a strange aspect, and the icon at the left of the text box is strange as well. Here is the latest bit of code:  </p>
  
  <pre class="lang-js prettyprint-override"><code>success: 
      function (data, textStatus, XmlHttpRequest) 
      {
          var result = data.d.results[0];
          //alert(result.new_LegalEntityId.Name);
          var parentFundLookup = [{ entityType : "new_LegalEntity", id : result.new_LegalEntityId.Id, name : result.new_LegalEntityId.Name}];    
          Xrm.Page.getAttribute("new_fundlegalentityid").setValue(parentFundLookup);
      },
  </code></pre>
  
  <p>I suspect that the problem lies in the <code>entityType : "new_LegalEntity"</code>, but I don't know what to put in there. Any clue on this ? What does that represent ?<br>
  <a href="http://goo.gl/TdaOB" rel="nofollow">Here is a screenshot</a> of the Legal Entity after the Sub-Fund is updated and the script has run.</p>