Disable CRM Form through JavaScript

I often get the question how to disable a form, so here it is:

The trick is to disable all the fields that are on the form. You can do this by using the following JavaScript:

if (crmForm.FormType == 2)
{
for( var i = 0 ; i < crmForm.all.length ; i++ )
{
if( crmForm.all[ i ].req)
crmForm.all[ i ].Disabled = true;
}
}

In the code I check if the form is in the update modus (FormType == 2), because I don't think that disabling a form while in create modus makes much sense. In the real world you'd probably disable a form depending on the value of another field.

Would you like to keep some fields editable, you can do the following:

if (crmForm.FormType == 2)
{
for( var i = 0 ; i < crmForm.all.length ; i++ )
{
if( crmForm.all[ i ].req && crmForm.all[i].id != "name")
crmForm.all[ i ].Disabled = true;
}
}

Here I disable all fields except the name field (crmForm.all[i].id != "name"). You can add other fields according to your needs. Just make sure you use the schema name of the fields you want to keep enabled.

Happy coding!

Kevin Dosseray

Comments

Popular posts from this blog

Scribe and Excel Data Ranges

Scribe MVP!