SBX - Search With Button

SBX - Forum Post Title

Javascript, onChange events no longer work on an empty field

Microsoft Dynamics CRM Forum

Abe Hussmann asked a question on 9 Oct 2013 2:34 AM
My Badges

Question Status

Suggested Answer

I just noticed that if you have a form field that triggers a Javascript function on the onChange event it no longer triggers the function if you clear the field, but ONLY when you change the field data. For instance; if you have a Name field populated with a name, and you remove the name - the function isn't triggered. It's left empty. You have to change the text inside the field.

Is this a bug? Please don't tell me this feature was removed? It was incredibly useful.

Reply
Guido Preite responded on 9 Oct 2013 3:32 AM
My Badges
Suggested Answer

Hi Hussman,

the onchange event is triggered when the value changes, including when you clear a field.

I think is your javascript code that is not correct, if you can post here we can help to check it.

Reply
Kim responded on 30 Oct 2013 9:06 AM
My Badges

I am having the same issue as described in the problem description.

This is my code that is on the OnChange event of the field languageid

function LanguageChanged()

{

var page = Xrm.Page;

if (page == null) return;

page.getAttribute("languageid").setSubmitMode("always");

page.getAttribute("titleid").setSubmitMode("always");

var obj = page.getAttribute("languageid");

if  (obj.getValue() == null)

{

page.getControl("titleid").setDisabled(true);

page.getAttribute("titleid").setValue(null);

       page.getAttribute("titleid").setRequired("none");

}

else

{

page.getControl("titleid").setDisabled(false);

       page.getAttribute("titleid").setRequired("required");

}

}

I think it is caused because this languageid field is required. If I clear it I get a warning that the field is blank, but the OnChange trigger is not fired.

Reply
Rodrigo Hentz responded on 19 Nov 2013 4:47 AM

I have the same issue. My script is correct. I created a business rule when the first field not contain data to  set the second field with the primary field, in this case, null. but this is not useful, two customizations to do the same thing. it is a bug or the product now works this way?

Reply
Anonymous picture
Anonymous
My Badges

I have the same issue for a CRM 2013 on-premise environment. I think the issue may be caused by the business requirement of the field.

If the field is business required, removing the value will not trigger onChange event.

Hope this will be fixed by Microsoft CRM team as soon as possible, because this is causing a lot of problems.

Regards,

Jaimie

Reply
S P responded on 3 Feb 2014 7:13 PM
My Badges
Suggested Answer

I have not tested this, but I it would be worth a try...remove the business required on the field, then set business required on the form OnLoad event instead using javascript. You would think that the form would do the equivalent when it loads, but who knows. The only problem is that you would need to add this logic to each form on the entity if it works.

Let me know how it goes!

Reply
Ferdy Griffioen responded on 4 Feb 2014 5:13 AM

I'm having the same problem.

When the field is 'optional' or 'recommended' the function passed in the 'addOnChange' function will be executed when the field is cleared. But when the field is 'required' and the user clears the field the onchange is not fired.

I think (note: not sure but 'I think') that it has something to do with the new 'field notifications' feature in CRM2013. What this does is put a nice cross in a hexagon when a required field is empty. This happens onChange and probably cancels out the onChange you/I place on the field via JS code.

I also tested it on CRM2011 to be sure (as this does not have the field notification feature) but here all works fine.

Going to search and maybe contact microsoft about it. Keep you posted.

Reply
Jonathan Wacks responded on 5 Feb 2014 2:52 AM

I just encountered this problem as well - my business required field doesn't trigger an OnChange when it's cleared.  Please let us know if you get a response from Microsoft!

Reply
Taichi Sato responded on 26 Feb 2014 5:27 PM
My Badges

I am having the same problem. Well, actually it doesn't matter as much in my case. But I would like to know that this behavior will be changed in future or not.

Reply
Stefano Miglietta responded on 17 Apr 2014 6:20 AM
Suggested Answer

You are right, if the field is required the event dont fire if you are removing the value....horrible.

Reply
Taichi Sato responded on 30 Apr 2014 1:51 AM
My Badges

1. Open a new record

2. Set a value "ABC" to a required field.

3. Move the focus

then, the onchange event is triggered!

4. Remove the value "ABC" from the field.

5. Move the focus.

then, the onchange event is NOT triggered...

But it's still okay becaue I can not save the record anyways because it is a required field.

The real problem comes next.

6. Set a value "ABC" to the field again.

then, the onchange event is NOT triggered!!

(In case, I haven't saved the record yet, I mean, the value "ABC" is definitely a changed value.)

My custom validation code in the onchange event is not fired, which doesn't allow the value "ABC" to be saved. Yes, I could set the validation in the onsave event but I want to show the notification when the value changes.

Reply
Ferdy Griffioen responded on 30 Apr 2014 2:06 AM

@Taichi Sato:

Step6 makes sense in a weird way.

Because you set the value 'ABC' in the field (@ step 2) the value on that particular moment is 'ABC'. On step 4 you remove the value and the onchange is not triggerd. But at this particular moment the value (in the backend) is still 'ABC' because (for some stupid reason) the backend value is not cleared. I think this is done because the entity will not save due to a required field being empty, and thus it doesn't matter what the backend value is of that field.

Reply
Taichi Sato responded on 30 Apr 2014 2:22 AM
My Badges

@Ferdy Griffioen

So, we arrived at the conclusion that the onchange should be triggered when I remove the value "ABC" at step4. If it is triggered, the onchange of step6 would be triggered too.

Reply
Ferdy Griffioen responded on 30 Apr 2014 2:26 AM

@Taichi Sato:

I think so yeah. But the problem is that it doesn't and I think Microsoft did this 'by-design'. Because why trigger a onchange (which cost a (tiny) bit of performance) while the user can't save the entity.

Reply
Taichi Sato responded on 30 Apr 2014 2:45 AM
My Badges

@Ferdy Griffioen:

Assuming that "ABC" is an invalid value, when I set "ABC" to the required field, the notification "Don't input ABC!!" is displayed by the code I wrote in the onchange event.

And I remove the value "ABC". Then, the notification is removed. (don't know why. I can't handle it anyways) And the notification is never displayed when I set "ABC" to the required field again. In this case, "ABC" could be saved.

I hope this will not be closed with 'by-design'....

Thanks for your reply.

Reply
Ferdy Griffioen responded on 30 Apr 2014 2:52 AM

@Tachi Sato:

I'm also working (at the moment) with the entire attribute setNotification etc and stumbled against the same problem (like 5 minutes ago).

I don't have a work around for it, but for our situation it is not that big of a deal.

But just like you I hope Microsoft doesn't closes this as a 'by-design'

Reply
Guido Preite responded on 9 Oct 2013 3:32 AM
My Badges
Suggested Answer

Hi Hussman,

the onchange event is triggered when the value changes, including when you clear a field.

I think is your javascript code that is not correct, if you can post here we can help to check it.

Reply
S P responded on 3 Feb 2014 7:13 PM
My Badges
Suggested Answer

I have not tested this, but I it would be worth a try...remove the business required on the field, then set business required on the form OnLoad event instead using javascript. You would think that the form would do the equivalent when it loads, but who knows. The only problem is that you would need to add this logic to each form on the entity if it works.

Let me know how it goes!

Reply
Stefano Miglietta responded on 17 Apr 2014 6:20 AM
Suggested Answer

You are right, if the field is required the event dont fire if you are removing the value....horrible.

Reply
ALTOSHAAM responded on 1 Dec 2014 2:14 AM
My Badges
Suggested Answer

Hi Hussman,

This issue was solved in Service Pack 1 (support.microsoft.com/.../2941390)

"JavaScript methods will not trigger when a business required field is changed to be blank."

Hope this helps.

Reply

SBX - Two Col Forum

SBX - Migrated JS