Click to Rate and Give Feedback
MSDN
MSDN Library
Microsoft Dynamics
 Form Event: OnSave
Form Event: OnSave
banner art

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:

EntityEvent ModeValue
AllSave1
AllSave and Close2
AllSave and New59
Task, Phone Call, Letter, Fax, AppointmentSave as Completed58
AllDeactivate5
AllReactivate6
All user-owned entitiesAssign47
E-MailSend7
LeadQualify16
LeadDisqualify15

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.


© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker