Skip to main content

Notifications

Announcements

No record found.

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

  • Suggested answer
    tpeschat Profile Picture
    tpeschat 4,926 on at
    RE: CRM 2016 Bug - Xrm.Page.getAttribute("Year").getValue(); does not work Correctly in OnChange Event and triggers twice the function logic

    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

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CRM 2016 Bug - Xrm.Page.getAttribute("Year").getValue(); does not work Correctly in OnChange Event and triggers twice the function logic

    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

  • awad Profile Picture
    awad on at
    RE: CRM 2016 Bug - Xrm.Page.getAttribute("Year").getValue(); does not work Correctly in OnChange Event and triggers twice the function logic

    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
    Pramod M Profile Picture
    Pramod M 1,445 on at
    RE: CRM 2016 Bug - Xrm.Page.getAttribute("Year").getValue(); does not work Correctly in OnChange Event and triggers twice the function logic

    Hello Awad,

    Try these 2 options, it may help you.

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

    community.dynamics.com/.../205194

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans