Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CRM 2011 onChange event handler not firing when field has been set by JavaScript

I'm adding some JavaScript to a CRM 2011 form and I'm seeing behaviour I don't understand. (I've radically simplified this example to get to the heart of the problem.)

I have a field called "new_myfield" which is radio button, and I'm firing a JavaScript function OnChange to set it to false. If I use the following script, all works out fine, I can run the script again and again, and every time it sets the field back to false:

var myField = Xrm.Page.getAttribute("new_myfield");
myField.setValue(false);

Of course, what's actually happening is the script is firing twice, because when the field is set to false, this again fires the script, which again sets it to false. I've verified this by putting alerts in the script.

But I actually only want to run this script when a user has set the field to 'true', so I change it to this:

var myField = Xrm.Page.getAttribute("new_myfield");
if (myField.getValue() == true) {
    myField.setValue(false);
}

The script runs once as expected and sets the field to 'false', but then the next time I manually set the radio button to 'true', the script doesn't run. It doesn't even fire. It's like the onchange event handler hasn't recognised anything has happened. It's only when I then manually set the button back to 'false' that the script runs again as expected.

It's as if the script needs to run through twice (as in the first example) for this to work.

Anyone any ideas why this is happening?

like image 762
Loz Avatar asked Jul 07 '16 09:07

Loz


1 Answers

Onchange event not firing when using setValue is an intended behavior by design, you need to explicitly call fireOnChange().

Updating an attribute using setValue will not cause the OnChange event handlers to run. If you want the OnChange event handlers to run you must use fireOnChange in addition to setValue. Source

like image 138
dynamicallyCRM Avatar answered Nov 14 '22 05:11

dynamicallyCRM