Wednesday, August 26, 2009

CRM: Disable Fields

A client wants to know: When creating custom solution in MSCRM is there a way to disable all the fields on a form (based on data entered)?

This is a frequent question while creating custom CRM solutions: How can I disable all or most of the fields on a CRM form based on data selected by the user.

The script below does this:

1: // disable all of the fields on the form.
2: DisableFormFields = function(onOff)
3: {
4: var iLen = crmForm.all.length;
5:
6: for (i = 0; i < iLen; i++)
7: {
8: o = crmForm.all[i];
9: switch (o.tagName)
10: {
11: case "INPUT":
12: case "SELECT":
13: case "TEXTAREA":
* 1: // disable all of the fields on the form.
2: DisableFormFields = function(onOff)
3: {
4: var iLen = crmForm.all.length;
5:
6: for (i = 0; i < iLen; i++)
7: {
8: o = crmForm.all[i];
9: switch (o.tagName)
10: {
11: case "INPUT":
12: case "SELECT":
13: case "TEXTAREA":
14: case "IMG":
15: case "IFRAME":
16: if (o.id != "leadqualitycode")
17: {
18: o.disabled = onOff;
19: }
20: break
21: default:
22: break;
23: }
24: }
25: }


This code should be placed in the form's OnLoad event. In the example, we're working with the Lead Entity. The DisableFormFields() function is actually attached to the OnChange event for the Lead Status Attribute. The value of Lead Status is checked and calls DisableFormFields(true) to disable the fields or DisableFormFields(false) to re-enable the fields.

Disabling the entire form is not good idea, so allow the user to change their mind. Looping through each of the form fields, we check the id of the field and while Lead Status is not encountered, the fields' disabled status is set to either true to disable the fields or false to re-enable them.

No comments: