Errors when programmatically attaching events in forms

Today I had a very weird error. I had two datetime fields on a form, one on the first tab and one one the second. Let’s call them date1 and date2 to make it easier.

I attached an event in the onload by using

crmForm.all.date1.attachEvent(“onchange”, myFunc1);

and for the date2 field:

crmForm.all.date2.attachEvent(“onchange”, myFunc2);

The weird part was that if I changed date1 first and then date2, myFunc2 didn’t fire. But if I first changed date2 and then date1, both fired as they should.

So I tried just adding an alert instead using the normal onchange method via the form editor in CRM and that triggered every time, just as it should, so something is different.

The simple solution seemed to be to move date2 from the second tab to the first. That got everything working just as it should.

I havn’t tried it but I think that one could also write a method that confirms all the event attachments and then calling this at the end of every onchange triggering function and perhaps some extra time when the tabs are changed to make sure the events are bound correctly.

I have checked the documentation in CRM for if this kind of runtime event attachment might not be supported but my interpretation of the text is that is should be all right according to it. The closest paragraph in the unsupported customization section is the following:

“The use of custom HttpModules to inject HTML/DHTML into the Microsoft Dynamics CRM Forms. “

As I wrote, my interpretation is that attaching events (not even overwriting the onchange), should be ok according to this since we arn’t injecting anything, just adding an event listener. Hence I feel that this must be interpreted as a bugg in CRM.

Gustaf Westerlund
Microsoft Dynamics CRM Architect

Logica
www.logica.com