I have a form in my model-driven app (9.0) that has an Amount field. When saving the form, I need to do a lookup on the database for the user's Limit amount. If the Amount exceeds their Limit, I need to prevent the save from happening. I'm using
If you're having issues with "prevent default" like I was, I liked the simplicity of setting notifications on fields:
if(remaining < 0) {
formContext.getControl("rsf_type").setNotification('Cannot enter a PTO request. No remaining balance.');
} else {
formContext.getControl("rsf_type").clearNotification();
}
Your ideas are good ones. Thank you. They led me to my solution which is to get the user limit value in the OnLoad event for the form and save it in a global JS variable. Then, in OnSave, I can make the comparison without need to make the async retrieveRecord call.
Hi @tschopp ,
Since the callback added with addOnSave doesn't work with promises, the only way you can go is stopping the Saving while it waits for the promise, as @a33ik 's Blog shows.
In my experience, this might cause some issues when the standard functionality is calling the formContext.data.save, since this promise will always get rejected, and the promise.then will never be executed (even if the save is restarted after the async request, the original promise gets rejected). Some examples of issues are the BusinessProcessFlows or some standard ribbon buttons.
The sdk recommends to implement the saving validation using a PlugIn, in case the data is not on the form. This doesn't work good in some cases where you have to react to it and show the user some choices, but it seems to me that in your case a PreValidate PlugIn might be a good choice.
Hope it helps.
Kind regards,
Diana
----------
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."
Hello,
You can check my post that describes scenario that is pretty similar to yours - https://butenko.pro/2018/11/15/cancelling-save-based-on-the-result-of-async-operation/
Both the onsave event and the retrieve call supports promises. So if you make your onsave function async and await your retrieve call you can treat the retrieve call as synchronous.
So the code would look like
async function OnSave(){
....
var response = await Xrm.WebApi.retrieve(..)
...
}
Learn more about async await here https://javascript.info/async-await
Also consider using typescript instead, makes it easier to maintain the code
WarrenBelz
69
Most Valuable Professional
mmbr1606
51
Super User 2025 Season 1
Michael E. Gernaey
35
Super User 2025 Season 1