Skip to main content

Notifications

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.

  • 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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,099 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,866 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans