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

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

How to prevent field onchange event from getting fired in a loop.

(0) ShareShare
ReportReport
Posted on by 402

I am updating optionset value inside onchange function to the previous selection after showing an alert. which works fine but the function is again trigered due to this update. How can I prevent this.

Below id my code:

//registered on change of new_decision field

function onchange(){

var formContext = eContext.getFormContext();

alert("This is not a valid decision for application");

formContext.getAttribute("new_decision").setValue();

}

I have tried few things like atttaching onchange through addonchange method insted of Form UI.

I have also turned on legacy form rendering but it is firing in a loop.

I have the same question (0)
  • Suggested answer
    Mohammed Fakhri Profile Picture
    402 on at
    RE: How to prevent field onchange event from getting fired in a loop.

    Thanks Ankit and Henry.

    Ankit, I liked your solution (havent tried though) but I used SetTimeOut function to my rescue. -

    Use this to set value - window.setTimeout(function () { formContext.getAttribute("new_decision").setValue(); }, 300);

    This wont trigger the OnChange again in a loop. 

  • Henry J. Profile Picture
    5,237 on at
    RE: How to prevent field onchange event from getting fired in a loop.

    Hi Mohammed,

    Please refer to this article on Attribute OnChange Event as well:
    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/events/attribute-onchange

    Henry

  • Suggested answer
    Ankit Sabharwal Profile Picture
    on at
    RE: How to prevent field onchange event from getting fired in a loop.

    Hello Partner,

    If this is one time execution, like after alert you set the right value and you don't want to fire the OnChange event, you can fiddle with the code as follows :-

    var checkFlag = true;
    function onchange(){

    if(checkFlag)
    {
    var formContext = eContext.getFormContext();

    alert("This is not a valid decision for application");

    formContext.getAttribute("new_decision").setValue();
    checkFlag= false;
    }

    }

    Hope this helps.

    Best regards,

    Ankit

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Sahan Hasitha Profile Picture

Sahan Hasitha 225

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 129 Super User 2025 Season 2

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 125 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans