Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

OnSave Event

CRM 1.2
banner art

The OnSave event is fired when a user presses the Save or Save and Close button on the form. The event is fired whether the data in the form has been modified or not.

Validating data is a common reason to use the OnSave event. The OnSave event can cancel the save operation before it is sent back to the server. To cancel the save operation, the script should return false as shown in the following code.

event.returnValue = false;

Example

The following code shows how to validate the job title field when the save button is clicked and to provide a default job title when the save and close button is clicked.

var CRM_FORM_SAVE_MODE_SAVE = 1;
var CRM_FORM_SAVE_MODE_SAVEANDCLOSE = 2;

// Validate only if the user clicked "Save".
switch (event.Mode)
{
  case CRM_FORM_SAVE_MODE_SAVE:
  
    // If the user provided a first and last name, they must provide
    // a job title as well.
    if (crmForm.all.jobtitle.DataValue == "" &&
      crmForm.all.firstname.DataValue != "" &&
      crmForm.all.lastname.DataValue != "" &&)
    {
      // Tell the user what is wrong.
      alert("Please provide a Job Title for this person.");

      // Give the control focus.
      crmForm.all.jobtitle.SetFocus();

      // Cancel the save operation.
      event.returnValue = false;
      return false;
    }
  
    break;
  
  case CRM_FORM_SAVE_MODE_SAVEANDCLOSE:
    
    // If the user forgot to provide a job title, set a default title.
    if (crmForm.all.jobtitle.DataValue == "")
    {
      // Set a default Job Title.
      crmForm.all.jobtitle.DataValue = "N/A";
      
      // Because this is a "Save and Close",
      // just save the form.
      return true;
    }    
  
    break;
}

© 2007 Microsoft Corporation. All rights reserved.


Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.