title: How do I update foreign key with odata json
body: <p>Expected behavior: User clicks on Register button and record is created in related entity. This has worked up til now but now we have requirement to update the foreign key based on a lookup on the current form. How do I accomplish this?  The pasted code below throws error in browser "Error on the creation of record; Error – Bad Request"</p>
  
  <pre><code>// JScript source code
  CreateButton = function () {
      var fieldTable = crmForm.all.new_registerbutton_d;
      var html = "&lt;TABLE border=0 cellSpacing=0 cellPadding=0&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width=0px&gt;" + fieldTable.innerHTML + "&lt;/TD&gt;&lt;TD width=200&gt;&lt;INPUT style='BACKGROUND-COLOR: #d8e8ff' onclick=Button_OnClick() value='Register' type=button&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;";
  
      fieldTable.innerHTML = html;
      document.all.new_registerbutton.style.display = 'none';
      crmForm.all.new_registerbutton_c.innerText = "";
  }
  
  Button_OnClick = function () {
  
      //var new_se_registration = new Object();
      var invitationName = Xrm.Page.getAttribute("new_name").getValue();
      //new_se_registration.new_name = invitationName;
  
      var invitationGUID = Xrm.Page.data.entity.getId().replace('{', '').replace('}', '');
      alert(invitationGUID);
      var value = new Array();
      value[0] = new Object();
      value[0].id = invitationGUID;
      value[0].name = invitationName;
      //var invitation = {
      //id: invitationGUID, name: invitationName, entityType: "new_se_registration"
      //};
      //var invitation= { Id: invitationGUID, LogicalName: "new_se_registration", Name: invitationName };
      //value[0].entityType = Xrm.Page.data.entity.getEntityType();
      //new_se_registration.new_new_se_invitation_new_se_registid.setValue([{ id: invitationGUID, name: invitationName, entityType: "new_se_registration"}]);
      //new_se_registration.new_new_se_invitation_new_se_registid = invitation;
  
      //new_se_registration.new_eventname= Xrm.Page.getAttribute("new_eventname").getValue();
      //new_invitationid is the Event id
      var lookupItem = new Array;
      lookupItem = crmForm.all.new_invitationid.DataValue;
      var eventname = lookupItem[0].name;
      //new_se_registration.new_eventname = eventname;
  
      lookupItem = crmForm.all.new_invitationid.DataValue;
  
      var new_se_registration = {
  
          new_new_se_invitation_new_se_registid: {
          __metadata: { type: "Microsoft.Crm.Sdk.Data.Services.EntityReference" },
          Id: invitationGUID,
          LogicalName: invitationName
          },
  
          new_name: invitationName,
          new_eventname: eventname
      };
  
      //alert (  eventname);
  
      // end Ek - adding event name
  
      //deleteRecord(new_se_registration.new_name, "new_se_registrationSet");
  
      var reg = Xrm.Page.getAttribute("new_registered").getValue();
  
  
  
      if (reg == null) {
          reg = "N";
      }
  
  
      if (reg != "Y") {
  
          //Xrm.Page.getAttribute("new_registered").setValue("Y");
          //alert(reg);
          //return;
  
          createRecord(new_se_registration, "new_se_registrationSet", RegisterCompleted, null);
          Xrm.Page.getAttribute("new_registered").setValue("Y");
          //Xrm.Page.data.entity.attributes.get("new_registered").setSubmitMode("always");
          //Xrm.Page.data.entity.save(); 
      }
      else {
          alert("\"" + Xrm.Page.getAttribute("new_name").getValue() + "\" has been registered already.");
      }
  }
  
  
  function RegisterCompleted(data, textStatus, XmlHttpRequest) {
      var new_se_registration = data;
      Xrm.Page.data.entity.save();
      alert("\"" + Xrm.Page.getAttribute("new_name").getValue() + "\" has been registered successfully.");
  }
  
  function checkDuplicate() {
      alert("test");
  }
  
  function deleteRecord(id, odataSetName) {
  
      var serverUrl = Xrm.Page.context.getServerUrl();
      var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
  
      $.ajax({
  
          type: "POST",
          contentType: "application/json; charset=utf-8",
          datatype: "json",
          url: serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + id + "')",
          beforeSend: function (XMLHttpRequest) {
  
              //XMLHttpRequest.setRequestHeader("Accept", "application/json");
              XMLHttpRequest.setRequestHeader("X-HTTP-Method", "DELETE");
          },
  
          success: function (data, textStatus, XmlHttpRequest) {
              alert("Record deleted successfully!");
          },
  
          error: function (XmlHttpRequest, textStatus, errorThrown) {
              alert("Error while deletion – " + errorThrown);
          }
      });
  }
  
  
  function createRecord(entityObject, odataSetName, successCallback, errorCallback) {
  
      var jsonEntity = window.JSON.stringify(entityObject);
      var serverUrl = Xrm.Page.context.getServerUrl();
      var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
  
      $.ajax({
  
          type: "POST",
          contentType: "application/json; charset=utf-8",
          datatype: "json",
          url: serverUrl + ODATA_ENDPOINT + "/" + odataSetName,
          data: jsonEntity,
          beforeSend: function (XMLHttpRequest) {
              XMLHttpRequest.setRequestHeader("Accept", "application/json");
          },
  
          success: function (data, textStatus, XmlHttpRequest) {
              if (successCallback) {
                  successCallback(data.d, textStatus, XmlHttpRequest);
              }
          },
  
          error: function (XmlHttpRequest, textStatus, errorThrown) {
  
              if (errorCallback)
                  errorCallback(XmlHttpRequest, textStatus, errorThrown);
              else
                  alert("Error on the creation of record; Error – " + errorThrown);
          }
  
      });
  
  }
  </code></pre>