Whenever you provide fields that already have values, you reduce the time everyone spends typing and improve their accuracy. They can still override the default values, if necessary.
You set default values for Microsoft Dynamics CRM fields by writing JScript code that is triggered when a form loads or when a field value changes. The examples in this article introduce client-side programming using JScript. The code samples are simple to understand, copy, and modify, even if you've never written code before.
The first example, "Set the default duration of an appointment," shows how to change an appointment's default duration. This is a simple way to prevent errors that occur when employees make changes as they enter appointments.
The second example, "Set the due date based on the priority," shows how to change the value in the Due Date field for a task based on a value that the person selects in the Priority list on the Task form. Using the value of one field to set another field's default value ensures that employees follow a standard process. It also reduces the number of clicks and typing for each person.
To make the changes suggested in this article, you must have the System Administrator or System Customizer security role or equivalent privileges.
Set the default duration of an appointment
The following example shows how to use the OnLoad event to change the default value for an attribute when Microsoft Dynamics CRM loads a form. When the form is opened, Microsoft Dynamics CRM runs code that you've specified in the OnLoad event before the form appears. This ensures that employees always see the custom value.
Configure the OnLoad event for the Appointment form
- In the Navigation Pane, click Settings, click Customization, and then click Customize Entities.
- Double-click Appointment.
- Under Details, click Forms and Views, and then double-click Form.
- In the Common Tasks area, click Form Properties.
- Select OnLoad and then click Edit. The Event Detail Properties dialog box opens.
- Paste the following code into the box on the Event Detail Properties dialog box.
// Set the default duration to 60 minutes when the form is opened.
var CRM_FORM_TYPE_CREATE = "1"; var iMinutes = 60;
if (crmForm.FormType==CRM_FORM_TYPE_CREATE)
{
crmForm.all.scheduleddurationminutes.DataValue = iMinutes;
}
- Select the Event is enabled check box.
- Click OK to close the Event Detail Properties dialog box, and then click OK to close the Form Properties dialog box.
- On the Form: Appointment form, on the Preview menu, click Create Form.
- In the Create Form form, verify that Duration is set to one hour, and then close this window.
- On the Preview menu, click Update Form. Because the code runs only when the form is opened to create a new record, the value for Duration does not change when the form is opened to update a record. Close this window.
- On the Form: Appointment form, click Save and Close.
- If you are finished making changes, click Save and Close on the Entity: Appointment form, select Appointment in the Customize Entities list, and then from the Actions menu, click Publish.
Because Web pages are cached, published customizations might not be immediately visible. If this happens, you can close and re-open Microsoft Dynamics CRM or refresh the page by pressing F5.
Set the due date based on the priority
This example shows how to use the priority of the task to determine when the task is due. You see how to set a default value for one field when another value is changed.
The Priority field in the Task entity is a drop-down list, and drop-down lists have default values. When a new task opens, the priority is set to Normal. The example uses the OnLoad event to set the due date when the form is opened. It then uses the OnChange event for the Priority field to set the due date when the value is changed in the Priority field.
Configure the OnLoad event for the Task form
- In the Navigation Pane, click Settings, click Customization, and then click Customize Entities.
- Double-click Task.
- Under Details, click Forms and Views, and then double-click Form.
- In the Common Tasks area, click Form Properties.
- Select OnLoad and click Edit.
- Paste the following code into the box on the Event Detail Properties dialog box:
var CRM_FORM_TYPE_CREATE = "1";
var DUE;
var TODAY;
// Only make these changes when the form is opened in Create mode.
if (crmForm.FormType==CRM_FORM_TYPE_CREATE)
{
// Get today's date.
TODAY = new Date();
// Set the due today date to today + 3 days at 5:00 P.M.
DUE = TODAY.setDate( TODAY.getDate() + 3);
DUE=TODAY.setHours(17,0,0);
crmForm.all.scheduledend.DataValue = DUE;
}
- Select the Event is enabled check box.
- Click OK to close the Event Detail Properties dialog box, and then click OK to close the Form Properties dialog box.
- On the Form: Task form, on the Preview menu, click Create Form.
- Verify that the value for the Due field is set to three days from today at 5:00 P.M., and then close the Create Form window.
- Click Save, and then continue to the next procedure.
Configure the OnChange event for the Priority field on the Task form
- With the Form: Task form open from the previous procedure, click Priority, and then in the Common Tasks area, click Change Properties.
- On the Events tab, select onChange, and then click Edit. The Event Detail Properties dialog box opens.
- Paste the following code into the box on the Event Detail Properties dialog box:
var CRM_FORM_TYPE_CREATE = "1";
var PRIORITY_HIGH="High";
var PRIORITY_NORMAL="Normal";
var PRIORITY_LOW="Low";
var DUE; var TODAY;
// Only make these changes when the form is opened in Create mode.
if (crmForm.FormType==CRM_FORM_TYPE_CREATE)
{
TODAY = new Date();
// If Due Date is null, set it to today's date.
DUE = crmForm.all.scheduledend.DataValue;
if (DUE == null)
{
DUE = new Date();
}
// Set the due date based on the value of the Priority field.
// Set the time to 5:00 P.M. in each case.
switch (crmForm.prioritycode.SelectedText) {
case PRIORITY_HIGH:
DUE = TODAY;
DUE = DUE.setHours(17,0,0);
break;
case PRIORITY_LOW:
DUE = TODAY.setDate( TODAY.getDate() + 7);
DUE = TODAY.setHours(17,0,0);
break;
case PRIORITY_NORMAL:
DUE = TODAY.setDate( TODAY.getDate() + 3);
DUE = TODAY.setHours(17,0,0);
break;
}
crmForm.all.scheduledend.DataValue = DUE;
}
- Select the Event is enabled check box.
- Click OK to close the Event Detail Properties dialog box, and then click OK to close the Field Properties dialog box .
- On the Form: Task form, on the Preview menu, click Create Form.
- In the Priority box, select each value, and then verify that the value in the Due field changed correctly.
- Close the Create Form form.
- On the Preview menu, click Update Form, and then verify that changing the value in the Priority box does not change the value in the Due field.
- Close the Update Form form.
- On the Form: Task form, click Save and Close.
- If you've finished making changes, click Save and Close on the Entity: Task form.
- In the Customize Entities list, select Task, and on the Actions toolbar, click Publish.
Because Web pages are cached, published customizations might not be immediately visible. If this happens, you can close and re-open Microsoft Dynamics CRM or refresh the page by pressing F5.
Tips for modifying the JScript code
As you create default values in Microsoft Dynamics CRM, keep these points in mind:
- JScript is case-sensitive.
- Comments begin with //.
- Because the box on the Event Detail Properties dialog box where you enter the code accepts only plain text, it doesn't have an Undo function and doesn't check your JScript syntax. If you have syntax errors in the code when you preview your change, the only message you may see is in the Status bar: "Error on this page."
At a minimum, keep a copy of your code in Notepad so you can add and test one or two lines at a time. If you have the Microsoft Visual Studio development system installed, open a .js file and write your code there. Microsoft Visual Studio checks the JScript syntax.
- You must use the field names, not the display names, in your code. To find the name of a field on a form, select the field, select Change Properties, and click the Schema tab. To find the schema name for locked fields, click the Attributes tab for the entity, which includes a column for schema name and display name.
- Microsoft Dynamics CRM forms have properties and methods that you can call from your JScript code to determine such things as whether the form is opened for creating or updating a record. Fields also have properties and methods. Different types of fields have different properties. All field types have a DataValue property that you can use to return the value of a field or to set the value of the field. To learn about the properties and methods of Microsoft Dynamics CRM forms and fields, read the Form Object Model section of the Microsoft Dynamics CRM SDK.
- The JScript User's Guide and JScript Language Reference are helpful for looking up JScript syntax.