web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM 2016 Bug - Xrm.Page.getAttribute("Year").getValue(); does not work Correctly in OnChange Event and triggers twice the function logic

(0) ShareShare
ReportReport
Posted on by

Hi Team,

I have registered a Javascript function "validateYear" on the OnChange event of the year field. I am doing a validation like if the year field has less than 4 characters then alert the user.  

Action Performed : On the Form, i entered year as 12  

Expected Result

validateYear function is triggered and execute #line from 2 to 6. 

execute #line 7 and sets the year as empty string. so this will again trigger that validateYear function. This time when the #line 2 gets executed it is expected to  have the year as empty string and it should not execute the line 3.

Actual Result : 

validateYear function is triggered and execute #line from 2 to 6. 

execute #line 7 and sets the year as empty string. so this will again trigger that validateYear function. This time when the #line 2 gets executed it still holds the year value as 12 .

Code Below : 

  1. function validateYear() {
  2.    var year = Xrm.Page.getAttribute("year").getValue();  //This is not getting the right value when the event is again triggered due to change in value of the year on line 7.
  3.    if (year != null && year != "") {
  4.          if (year.length < 4) {
  5.              //Alert
  6.              Xrm.Utility.alertDialog("Please enter a valid Year", function () { });
  7.              Xrm.Page.getAttribute("year").setValue(""); // This line will trigger the onchange event function validateYear
  8.              return;
  9.          }
  10. }
  11. }

 I guess this is a Microsoft bug on the crm 2016 online 8.1 version. As anyone face this same issue ?

Thanks & Regards,

Awad.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Pramod M Profile Picture
    1,445 on at

    Hello Awad,

    Try these 2 options, it may help you.

    crmbusiness.wordpress.com/.../crm-2013-javascript-null-setting-oddity

    community.dynamics.com/.../205194

  • awad Profile Picture
    on at

    Hi Guido,

    Line 7 does getting triggered OnChange in the CRM 2016 online instance (8.1.0.563) (DB 8.1.0.538). And so i have the issue.

    You can try by writing a simple javascript, which just shows an alert and then clears the same field value on which this onchange is triggered.

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Awad,

    Faced this issue in CRM 2016. setValue() is triggering onChange(). Looks like this is a bug in CRM.

    As per MSDN, setValue() should not trigger onChange.

    https://msdn.microsoft.com/en-us/library/gg334409.aspx

    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.

    One workaround is turning turbo form off (turning legacy form rendering ON ) using Settings > Administration > System Settings > General.

    But turning turbo forms off has some other side effects and issues.

    Another workaround is using JavaScript.

    Can you try the below code:

    var isSettingYear = false;
    
    function validateYear() {
      if (isSettingYear) {
        return;
      }
      var year = Xrm.Page.getAttribute("year").getValue(); 
      if (year != null && year != "") {
        if (year.length < 4) {      
          Xrm.Utility.alertDialog("Please enter a valid Year", function() {});
          isSettingYear = true;
          Xrm.Page.getAttribute("year").setValue(""); 
          isSettingYear = false;
          return;
        }
      }
    }

    Regards,
    Charmis

  • Suggested answer
    tpeschat Profile Picture
    4,930 on at

    Yes that seems to be a bug. What about setting the variable year to an empty string after execution of line 7 as a workaround? Then the lines 4 to 6 should not be executed again

    Hth Thomas

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ZS-27011425-0 Profile Picture

ZS-27011425-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans