Use Case Corner—Adding Java Script to the OnChange Event of Picklist Fields

As you navigate through the field properties dialog boxes for the various field types, you'll notice that there is an additional tab labeled Events on the dialog box for picklist fields. This tab enables you to enter JavaScript that will be run during the OnChange event of this field. In other words, this enables you to run a piece of JavaScript code each time the value of this field is changed.

NOTE

If you are really daring, it is also possible to add JavaScript to nonpicklist fields by updating the Microsoft CRM XML files directly. However, this is definitely not a supported customization so proceed at your own risk.


We'll take advantage of this functionality by using it to require that a value be entered into the Contribution field when the user selects 3rd Form from the Starting From picklist.

Open the Starting Form field properties dialog box by double-clicking the field on the design form. Select the Events tab, select OnChange from the event list, and click the edit button. Figure 11.37 shows the edit screen with our code included.

Figure 11.37. Adding Java script to a picklist field's OnChange event.


On the Details tab check the Event enabled check box and enter the following code into the text window:

if (crmForm.CFPStartingForm.value == "3rd Form") {
    crmForm.SetFieldReqLevel("CFCContribution", true);
} else {
    crmForm.SetFieldReqLevel("CFCContribution", false);
}

In this code example, the second parameter (1 in this case) passed into the SetFieldReqLevel function tells the function whether to make the value required or not. In this case, a value of 2 would make the field not required.

NOTE

After reading through this example you may want to know how you can get a list of the javascript functions (such as SetFieldReqLevel) that are available when writing code in the on change event. As of this writing, there is no official reference of this information. The only way to figure out what functions are available is to view the source of the generated HTML page to get the list of included javascript files and then to study the files where they are used. Microsoft has done a good job at documenting the API but, to our knowledge, has not documented the client side functions.


Because the rule we are implementing is dependent on the Contribution field, we need to select the Dependencies tab and add that field as a dependency. Doing this will disallow the removal of this field from the form.

Now we can return to Deployment Manager, publish our customization, and test it out. Figure 11.38 shows the error message the user will receive when selecting 3rd Form and leaving the Contribution field blank.

Figure 11.38. Creating a dynamic field requirement by adding JavaScript to a picklist field's OnChange event.


One thing to keep in mind with this type of picklist customization is that the code we have written is dependent on the text value of the picklist option triggering the event. In other words, if we were to change the value of our option from 3rd Form to 10th Grade our code would stop working.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.211.110