Form Event: OnSave
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.
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 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.
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 == 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;
}
break;
}See Also
Concepts
Other Resources
© 2009 Microsoft Corporation. All rights reserved.