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

Notifications

Announcements

No record found.

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
    Ankit Sabharwal Profile Picture
    on at

    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.

  • Henry J. Profile Picture
    5,237 on at

    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
    Mohammed Fakhri Profile Picture
    402 on at

    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. 

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
iampranjal Profile Picture

iampranjal 41

#2
Martin Dráb Profile Picture

Martin Dráb 38 Most Valuable Professional

#3
Satyam Prakash Profile Picture

Satyam Prakash 35

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans