Async OnSave events
In a model-driven app form, to do any validations on record save, we use OnSave event of the form. We bind the event handler function by directly adding it in form properties OnSave or by binding the handler function using addOnSave (formContext.data.entity.addOnSave). When the record is saved (user clicked on Save / Save&Close or auto-save was triggered or save triggered from code using formContext.data.entity.save / formContext.data.save / formContext.data.refresh(true…)), the OnSave handler function is called and we have to use the formcontext.geteventargs().preventdefault() block the default save, perform validations and then trigger another save. We also have to handle what happens after saved depending on how the Save was triggered. As you can see adding custom logic to OnSave event to perform any validation is not very straightforward and not a clean approach and performing any async calls makes it even more complicated.
With the release of 2021 Wave1, Async OnSave feature will allow a promise to be passed to perform validations and if the promise does not resolve within 10 seconds, the save will be blocked. This provide a lot more cleaner pipeline to hook into OnSave with a flexibility to perform complex validations. I have tried to do some tests on my trial instances but the feature is still not available on my environment. I will update this post with sample code once I had a chance to play with it.
Public Preview – Feb 1, 2021 | Early Access – N/A | General Availability – April 2021
What are Async OnSave events?
“This provides the ability to create Async OnSave events. Async OnSave events will now wait when they provide a promise, and there is also a new timeout if the promise does not resolve within 10 seconds. If it does not resolve within 10 seconds, the save operation will be blocked.“
Some useful links related to OnSave event