I am at my wits end with this one. I have a form in my model-driven app with custom scripting and an event handler for the onSave event of the form. It does not appear to be triggering consistently, if at all. I have the top of the event handler pasted below, where I have a breakpoint set. I can change a field, see the unsaved message, click the Save button, my form will still save and my breakpoint never hits and I never see my notification toast pop up. I have seen it work before and I know that my mapping to the event handler in the form is correct. I can't seem to put together what in the event handler could be causing this type of behavior - specifically the fact that it doesn't appear to even fire.
Does anyone have any thoughts on what else I can look at?
var Javara = window.Javara || {};
(function () {
// Define some global variables
var toastId = "jav_recruitmentToast"; // Define an ID for the notification
// Code to run in the form OnLoad event
this.formOnLoad = function (executionContext) {
...
}
this.formOnSave = function (executionContext) {
var formContext = executionContext.getFormContext();
formContext.ui.setFormNotification("SAVING...", "INFO", toastId);
...
}
}).call(Javara);
So many great responses. I can't thank everyone enough. @sudipm , I definitely intend on using that pattern going forward. That said, I wanted to share what I ended up discovering as the root cause of my issue as I'm kind of curious as to folks thoughts on why it was manifesting the way it was - as in not making it into the event handler at all.
The issue was definitely related to my namespace. Later in the formOnSave handler, I had a call to one of my class' functions but was calling it like this: Javara.myMethod(executionContext). As soon as I changed it to this.myMethod(executionContext), everything worked fine and the event handler fired.
Essentially, I've learned the following based on the way I have defined my code with the namespace:
Hi @mhomol
Here is a pattern that has worked well for me, imo it makes the code more readable using classes.
//File : Javara.js
class Javara {
constructor(){
this.toastId = "jav_recruitmentToast";
}
formOnLoad(executionContext) {
....
}
formOnSave(executionContext) {
let formContext = exeutionContext.getFormContext();
formContext.ui.setFormNotification("SAVING...", "INFO", this.toastId);
....
}
}
window.Javara = new Javara(); //This line is important, it instantiates your class
Then in the form you will register your save handler like so
Hello,
I never used and understood how that approach with "call" works. For a while, I have used the following code and it always works fine for me:
var Javara = (function() {
var toastId = "jav_recruitmentToast"
function formOnLoad(executionContext) {
//...
}
function formOnSave(executionContext) {
var formContext = executionContext.getFormContext();
formContext.ui.setFormNotification("SAVING...", "INFO", toastId);
//...
}
return {
formOnLoad: formOnLoad,
formOnSave: formOnSave
};
})();
Hi @mhomol
In my previous entry I put the code that works in my environment put formOnSave as the name of the function
Sorry, I knew I posted too little last night, so apologies. I have updated the post with more information to denote how the "this" works with my namespace and how I have the events mapped in the form, as well as where my toastId is defined.
Sorry, I knew I posted too little last night, so apologies. I have updated the post with more information to denote how the "this" works with my namespace and how I have the events mapped in the form, as well as where my toastId is defined.
what is "this.formOnSave" ?? is this representing the "formContext" ?? you need formContext save async handler, as shown below.
https://community.dynamics.com/blogs/post/?postid=c4da8cbe-6d41-ee11-bdf3-00224827e5d4
https://temmyraharjo.wordpress.com/2022/10/15/model-driven-apps-add-on-post-save-event/
Hi @mhomol
You must define the toastId, I changed the code a little, it works for me with this change
function formOnSave (executionContext) {
var formContext = executionContext.getFormContext();
toastId = "INFO"
formContext.ui.setFormNotification("SAVING...", "INFO",toastId)}
mmbr1606
22
Super User 2025 Season 1
stampcoin
19
Michael E. Gernaey
17
Super User 2025 Season 1