Sample onSave event script

Can I do this task?

This JScript sample script shows how to use an onSave event to prevent a user from saving the form if the user doesn't enter required information. This takes validation one step further than is possible by setting a field on a form as Business Required, because the validation occurs only when specific data is entered in other fields. The script has different behavior when the user clicks Save or Save and Close.

var CRM_FORM_SAVE_MODE_SAVE = 1;

var CRM_FORM_SAVE_MODE_SAVEANDCLOSE = 2;

// Only validate if the user clicks "Save".

switch (event.Mode)

{

case CRM_FORM_SAVE_MODE_SAVE:

// If the user provides a first and last name, the user must provide

// a job title as well.

</font> if (crmForm.all.jobtitle.DataValue == null &&

crmForm.all.firstname.DataValue != null &&

crmForm.all.lastname.DataValue != null)

{

// Tell the user what must be provided.

alert("Please provide a Job Title for this person.");

// Give the control focus.

crmForm.all.jobtitle.SetFocus();

// Cancel the save.

event.returnValue = false;

}

break;

case CRM_FORM_SAVE_MODE_SAVEANDCLOSE:

// If the user forgot to provided a title, provide 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.

}

break;

}

Related Topics

Form Scripting

Customizing Entities

Configure Scripts for Form Events

Preview Form Customizations

Customizing Entities - Troubleshooting

Did you find the information that you need?
Yes      No 
If not, what information do you need? (optional)

© 2009 Microsoft Corporation. All rights reserved.