Form Event: OnSave

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

The OnSave event occurs when a user presses the Save or Save and Close button on the form. The event always occurs, even when the data in the form hasn't changed.

Use the OnSave event to validate data. To cancel the save operation, the script should return false as shown in the following code.

event.returnValue = false;

OnSave Event Modes

You can gain more detailed information about the user actions in the OnSave event by referencing the event.Mode values. These values represent the buttons clicked by the user to save the record.

The following event modes are supported:

Entity Event Mode Value
All Save 1
All Save and Close 2
All Save and New 59
Task, Phone Call, Letter, Fax, Appointment Save as Completed 58
All Deactivate 5
All Reactivate 6
All user-owned entities Assign 47
E-Mail Send 7
Lead Qualify 16
Lead Disqualify 15

Note   When users click Convert Lead… they are given the option to qualify or disqualify a lead.


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.


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

      // Give the control focus.

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

See Also


Other Resources

© 2010 Microsoft Corporation. All rights reserved.