- Change the default duration from 30 minutes to blank.
- Auto-populate today’s date into the Due Date field.
These two changes might seem simple at first, but digging deeper into the details of these two fields reveals some obstacles to make these changes. Because the Duration field looks like a picklist field, you would expect to be able to just change the default value by editing the Duration attribute of the Task entity. However, even though the Duration field appears to be a picklist, it is actually a special field used on various activities in Microsoft CRM. For these special fields, you can not set a default value (or add picklist values) like you can for normal picklist fields.
In addition to the Duration field nuance, DateTime attribute data types do not have a default value. Since the Due Date field uses the DateTime data type, we can’t use the attribute editor to specify today’s date as the default value in the Due Date field.
Despite the two problems we just outlined, we can easily meet the customer’s request to change the default task form by leveraging the form OnLoad event of the task entity’s form.
To accomplish this we will add a simple script to the form OnLoad event and Microsoft CRM will run our script everytime a user opens the task form. When it runs, our script will programmatically change the value of the Duration and Due Date fields to the values we specify
Procedure: Using the Form OnLoad Event to Default Task Duration and Due Date
1. Navigate to Settings -> Customization -> Customize Entities. Double click the Task entity. The
entity editor will appear.
entity editor will appear.
2. Click Forms and Views. Double click Form. The form editor for the task entity will appear.
3. Click Form Properties and the editor window will appear. From here, you see that you can choose to add a script to the form OnLoad or OnSave event.
| 4. | Select OnLoad and click Edit. The Event Detail Properties window will appear. In this window, enter the following code:var CRM_FORM_TYPE_CREATE = 1;
var CRM_FORM_TYPE_UPDATE = 2;
switch (crmForm.FormType)
{
case CRM_FORM_TYPE_CREATE:
crmForm.all.actualdurationminutes.DataValue = null;
crmForm.all.scheduledend.DataValue = new Date();
break;
case CRM_FORM_TYPE_UPDATE:
// do nothing
break;
}
A quick review of this code shows that Microsoft CRM allows us to programmatically set the values on the form using javascript. This code sets the actualdurationminutes field (the schema name for Duration) to NULL, which means the same as blank. It also sets the scheduledend field (the schema name for Due Date) to today’s date using the javascript Date( ) function. The OnLoad event will run this script everytime a user opens a form, but remember we only want to set the default values when users create a new task. Therefore, we added the cases so that our script only runs for the form type of 1. Microsoft CRM assigns a form type value to each form (1-Create, 2-Update, 3-Read Only, etc.) so that you can tie this information into your scripts just like we needed in this example. In this script, if the form type is 2 that indicates it is an update form so we don’t alter the Due Date and Duration field values. 5. After you enter the code, click the Event is enabled checkbox. This box indicates whether or not Microsoft CRM should run your script when it fires the OnLoad event. The screen should look like this: |
| 6. Next we’ll setup the Dependencies related to our code to make sure that no one accidentally removes the Duration or Due Date field from the task form (because our script would generate an error if those fields were missing). Click the Dependencies tab. 7. You’ll see two columns: Available Fields and Dependent Fields. The Available Fields column lists all of the fields that appear on the task form. To make Due Date and Duration Dependent Fields, select those values in the left column and then click the ">>" button. Microsoft CRM will move those fields into the Dependent Fields column. |
| . | |||
| 8. Click Ok. You should be back on the task form editor now. | |||
| 9. Next we want to use the form preview feature to test the code we just added. Before you try preview, make sure you save the changes you made by clicking the Save button.
| |||
No comments:
Post a Comment