Friday, July 8, 2011

CRM 2011 Boolean OnChange event not working properly

If you've used CRM 2011 bit fields with OnChange events, you've probably noticed some very annoying behavior: The event is only fired when you leave the checkbox, not when the box itself is checked. That is, you have to check the box then click elsewhere for the Javascript to fire.

A blogger over at PowerObjects.com has a great CRM4 solution:
http://www.powerobjects.com/blog/2009/09/10/crm-bit-field-onchange-event-not-firing-as-expected/

The idea is that, instead of firing on the onchange event, we fire on onclick such as:

crmForm.all.checkbox.onclick = functionName;

This works great, and the function will fire the moment the checkbox is interacted with. However, we need to make sure that we use the old-style CRM4 method of accessing the bit rather than the new CRM2011 Xrm model. For whatever reason, attempting to access the Xrm model during onclick returns very erratic values -- in my experience, it would report false for several straight clicks, then true when the box was unchecked at one point! So, be sure to access the field in your function as such:

if(crmForm.all.checkbox.DataValue) {
  // Do something if true
} else {
  // Do something if false / null
}

Hopefully this helps you avoid the confusion I ran into!

No comments:

Post a Comment