OnChange Event Javascript

I frequently hear from people looking for samples of javascript code for onchange events in CRM. Here's a sample we use in our internal CRM system for customer service cases.

We use cases to track all of our time, since all of our time should be spent working on something for a customer or working on an internal project. On our case forms we have a casetype picklist with the following values:

Maintenance Contract
Billable Services

If the case type is Maintenance Contract, we want to make sure the technician enters the contract that the case is related to. I use the following code in the casetype event box on the case customization form to set the contract field to "required" if the case is a Maintenance Contract case:

switch (parseInt(event.srcElement.returnValue, 6))
if (crmForm.casetypecode.returnValue != "1"){crmForm.SetFieldReqLevel("contractid",0);}
if (crmForm.casetypecode.returnValue == "1"){crmForm.SetFieldReqLevel("contractid",1);}

The first line basically says there are six choices in the picklist (the five listed options plus the default null option). Then the code says that if the selected option is not equal to option 1, then set the contractid to Not Required. The remainder of the code says that if the selection is equal to option 1, then set the contractid field to Required.

When setting up the form, set the contractid field to be Not Required by default. Note that you need the line of javascript that says "If selection is not equal to option 1, set the contractid field to Not Required." Even though this field is not required by default, you want to take into account the times when the end user sets the field to Option 1 (making the contractid field required) and then changes his mind, or realizes he made a mistake. In that case, you want to make sure the contractid field gets set back to being not required.


uncertain_smile said...

I need HELP, please
You code is perfect when you want to make a "standard field" required, but when I try to make a picklist required I cannot make it work. The page keeps issuing an error saying "fieldname is undefined", where fieldname is the name of the picklist where the event is located. I have done everything I can, restart IIS, publish, etc... nothing happens
Can youi please provide me some feedback on this? Thanks

Edward said...


Your article describes how to set enable business required field. How can you do the same for a business recommended field ?



Matt Wittemann said...

As far as I know, the only options available to the Javascript OnChange event are to make the field required or not required. CRM 1.2’s support for Javascript is pretty basic, unfortunately…

Anonymous said...


I have a trouble, i don't now JavaScript very well, and i need make a customization, i need sum to fields, I guess program the script OnChange event of each textbox value. Could you show me some example of how to do this?
this are fields:
new_Total = estimatedvalue + valor2


Sole said...

Hi Matt,

Non Billable Hours

How would you go about entering non billable hours for a case ? I see you have a case type of 'Admin/Overhead'. Do you alter or add scripting to run upon Resolving a case to skip allotment re-calculation based on this type.

Any suggestions or comments will be much appreciated.


brenden said...

My event won't fire not even a Debugger; line will execute onchange

Matt Wittemann said...

That code was for CRM 1.2. What version of CRM are you using?

ICU MSCRM © 2004-2009