Wednesday, January 12, 2011

MS CRM events and custom code

We are working on a large implementation of MS CRM for a client that requires a sizable amount of custom code in the forms. Here is one that tripped us up a "bit."

When working with bit fields (yes/no), you can change the way they appear on the form, as either a checkbox, pulldown, or radio button. We had some fields setup as radio button so that when the user changes one radio button, it would update other fields on the form. We wanted to make the code easier to work with so we took it out of the form and put it into its own custom javascript file that is included into the form when it loads.

For all the rest of our forms, the way we would get a script to execute is to use the attachEvent() method, so that in our .js file, we have:

crmForm.all.new_field.attachEvent('onchange', some_custom_js_function);

but we noticed that this didn't work on the radio buttons. After a bunch of searching, we found that it is really the onlick event of a radio button that we needed to attach the event listener to, so that we get:

crmForm.all.new_field.attachEvent('onclick', some_custom_js_function);

We later discovered that using onclick worked very well for the checkbox too - it had a very distressing behavior that it was not firing onchange until after it lost focus, but it does fire onclick as soon as the user clicks on it.

Let me know if you found this helpful!