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...
Unanswered

Custom JS code on Save not working

(1) ShareShare
ReportReport
Posted on by 2
I am exploring things in model driven apps using js, so i want to validate the form using basic validation like is the fields empty etc. So I have a js code on form save but when i select save button for the first time without filling anything the errors show up (using set notification error code in js) but after I fill the field and click on submit, the code is not triggering again. I used the same code and instead of set notification i used alerts there it works fine but set notification thing doesn't work. Any idea what am i doing wrong. Here the code snippet.
 
function validateLeaveOnSubmit(executionContext) {
    var formContext = executionContext.getFormContext();
    var eventArgs   = executionContext.getEventArgs();
    var hasErrors   = false;
    var dummyCtrl = formContext.getControl("sm_dummy");
    dummyCtrl.clearNotification("sm_dummy_req");
    var dummyAttr = formContext.getAttribute("sm_dummy");
    var dummy     = dummyAttr.getValue();
    if (dummy === null) {
        dummyCtrl.setNotification("Please enter dummy value.", "sm_dummy_req");
        hasErrors = true;
    }
    if (hasErrors) {
        eventArgs.preventDefault();
    }
}
Categories:
I have the same question (0)
  • Suggested answer
    Inogic Profile Picture
    657 on at
    Custom JS code on Save not working
    Hi,
     
    You’re not doing anything wrong — this is expected behavior in model-driven apps. When you use setNotification together with preventDefault(), the save is cancelled, but Dynamics doesn’t always re-run the OnSave logic on the next save click unless the field becomes “dirty” again. Alerts always fire, but control notifications can stay cached.
    To fix this, run the same validation on the field’s OnChange event and clear the notification there. This ensures the control refreshes and the validation works properly on every save attempt.
    Hope this helps.
     
    Thanks!
    Inogic
     

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…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

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

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 57 Most Valuable Professional

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 52 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans