Whenever you provide fields that have values already entered for your users, you reduce the time they spend typing and improve their accuracy. Users can still override the default values, if necessary.
You set default values for Microsoft Dynamics CRM 3.0 fields by writing Microsoft 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 users 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 user selects in the Priority list on the Task form. Using the value of one field to set another field's default value ensures that users follow a standard process. It also reduces the number of clicks and typing for each user.
To make the changes suggested in this article, you must have the System Administrator or System Customizer security role.
This example shows how to use the onLoad event to change the default value for an attribute when Microsoft CRM loads a form. When a user opens the form, Microsoft CRM runs code that you’ve specified in the onLoad event before the form appears. This ensures that users always see the custom value.
Create an onLoad event for the Appointment form
1.
Click Settings, click Customization, click Customize Entities, and then double-click Appointment.
2.
Click Forms and Views, and then double-click Form.
3.
In the Common Tasks area, click Form Properties.
4.
Click onLoad, and then click Edit. The Event Details Properties form opens.
5.
Paste the following code into the box on the Event Details Properties form.
// 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;
}
6.
Select Event is enabled.
7.
Click OK to close the Event Detail Properties form, and then click OK to close the Form Properties form.
8.
On the Form: Appointment form, on the Preview menu, click Create Form.
9.
In the Create Form window, verify that Duration is set to one hour, and then close this window.
10.
On the Preview menu, click Update Form. Because the code runs only when a user opens the form to create a new record, the value for Duration does not change when the user opens the form to update a record. Close this window.
11.
On the Form: Appointment form, click Save and Close.
12.
If you are finished making changes, close the Entity: Appointment form, and then click Publish.
13.
Because Web pages are cached, published customizations might not be visible to your users. If this happens, ask them to refresh the page or to delete the temporary files for their Internet browsers.
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 a user changes another value.
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 user changes the value in the Priority field.
Add an onLoad event for the Task form
1.
Click Settings, click Customization, click Customize Entities, and then double-click Task.
2.
In the Common Tasks area, click Form Properties.
3.
Paste the following code into the box on the Event Detail Properties form:
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;
}
4.
Click OK to close the Event Detail Properties form, and then click OK to close the Field Properties form.
5.
On the Form: Task form, on the Preview menu, click Create Form.
6.
Verify that the value for the Due Date field is set to three days from today at 5:00 P.M., and then close the Create Form window.
7.
Click Save, and then continue to the next procedure.
Add an onChange event form for the Priority field on the Task form
1.
With the Form: Task form open from the previous procedure, click Priority, and then in the Common Tasks area, click Change Properties.
2.
On the Events tab, click onChange, and then click Edit. The Event Detail Properties form opens.
3.
Paste the following code into the box on the Event Detail Properties form:
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;
}
4.
Select Event is enabled.
5.
Click OK to close the Event Detail Properties form, and then click OK to close the Form Properties form.
6.
On the Form: Task form, on the Preview menu, click Create Form.
7.
In the Priority box, select each value, and then verify that the value in the Due Date field changed appropriately.
8.
Close the Create Form form.
9.
On the Preview menu, click Update Form, and then verify that changing the value in the Priority box didn't change the value in the Due Date box.
10.
Close the Update Form form.
11.
On the Form: Task form, click Save and Close.
12.
If you’ve finished making changes, close the Entity: Task form, and then click Publish.
13.
Because Web pages are cached, published customizations might not be visible to your users. If this happens, ask them to refresh the page or to delete the temporary files for their Internet browsers.
As you create default values in Microsoft CRM, keep these points in mind:
•
JScript is case-sensitive.
•
Comments begin with //.
•
Because the box on the Event Detail Properties form 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 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, keep a Visual Studio form open and write your code there. Visual Studio checks the JScript syntax as you program.
•
You must use the schema names, not the display names, of forms and fields in your code.
•
To find the schema name of a form, open the Customize Entities form, which includes a column for the schema name and display name for each entity. The form name is the same as the entity schema name.
•
To find the schema 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 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. Most 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 CRM forms and fields, read the Form Object Model section of the Microsoft CRM 3.0 SDK.
For additional samples of client-side JScript code, download the Microsoft CRM 3.0 SDK. The installation process creates a folder named sdk samples\reference\client side. That folder has a subfolder named dom with samples showing the document object model. It also has a subfolder named form events with samples showing how to use onSave, onLoad, and onChange events.