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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

Custom JS code on Save not working

(3) ShareShare
ReportReport
Posted on by 6
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
    748 on at
    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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
11manish Profile Picture

11manish 156

#2
Pallavi Phade Profile Picture

Pallavi Phade 102 Super User 2026 Season 1

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 55 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans