title: Refresh form after save and close
body: <p>I have some custom script that adds the latest note value to a hidden text field, and the hidden text field is subsequently displayed in one of my views for the entity. When I click save and close after adding a new note, the view does not update with the newest note since the form was not refreshed. What can I do so that the form will be refreshed if the user clicks on save and close? </p>
  
  <p>EDIT: @Arthur For some reason, I cannot add a comment to your post. </p>
  
  <p>Anyhow, I tried as you have suggested. When I tried to click on "save" alone, the form refreshed and the value returned was true, so the flag was tripped. However, when I tried the same with "save and close" the form did not refresh, and consequently the flag was not tripped. This is leading me to assume that I must refresh the form in order for the view to update.</p>
  
  <p>EDIT 2: Here is the code that retrieves the information.</p>
  
  <pre><code>function GetLatestNote() {
  
    var req = new XMLHttpRequest();
  
    req.open("GET", encodeURI(Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/AnnotationSet?" + "$select=NoteText&amp;$filter=ObjectId/Id eq guid'" + Xrm.Page.data.entity.getId() + "'&amp;$orderby=CreatedOn desc&amp;$top=1"), true);
  
    req.setRequestHeader('Accept', 'application/json');
  
    req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
  
    req.onreadystatechange = function() {
  
  
      if (this.readyState === 4) {
  
        req.onreadystatechange = null;
  
        if (this.status === 200) {
  
          var note = JSON.parse(req.responseText).d;
          var results = note.results;
          var NoteText = results[0].NoteText;
          var newnote = Xrm.Page.getAttribute("new_lastcomment").setValue(NoteText);
        }
      }
  
    };
  
    req.send();
  
  
  }
  </code></pre>
  
  <p>new_lastcomment is the hidden field. What I am trying to do now is that if the user clicks on save and close, to stop the save event, reload the page, and use the script to close the form. Here is the code for that function:</p>
  
  <pre><code>function save(executionObj) {
    var savestate = executionObj.getEventArgs.getsavemode();
    if (savestate == 2) {
      executionObj.getEventArgs().preventDefault();
      window.location.reload(true);
      Xrm.Page.data.entity.save("saveandclose");
    }
  }
  </code></pre>
  
  <p>What happens when I test this code however is that I get the following message when I attempt to save and close :</p>
  
  <p>There was an error with this field's customized event.Field: crmForm, Event: onsave, Error: Object does not support property or method 'getsavemode'</p>
  
  <p>However, once I click ok, the script closes and the view is updated as I want it! But now there is a problem because I do not want this error message popping up every time I hit save and close. To add more depth here, I run both of these function as an onsave event, and for the save function, I have "Pass execution context as first parameter" checked. I tried to run it without checking this box, and got an error that the value was undefined, but once again, after I clicked ok, the form would close and the view updated. Why is this error coming up now? </p>
  
  <p>EDIT: @Arthur The form is now refreshing, but I am still getting the same error message that I was getting before. </p>