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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / How to prevent save ev...
Power Apps
Unanswered

How to prevent save event when validation required database call?

(0) ShareShare
ReportReport
Posted on by 311

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 

Xrm.WebApi.retrieveRecord to make the database call, which is asynchronous.  So by the time I get the value back from the DB, the save has already happened.  Any ideas on how I can accomplish this?
 
Here's my javascript code that's wired to the OnSave event:
 
function CheckUserLimit(executionContext) {
    var formContext = executionContext.getFormContext();
    var globalContext = Xrm.Utility.getGlobalContext();

    var userId = globalContext.userSettings.userId.slice(1, -1);
    var enteredAmount = formContext.getAttribute("amount").getValue();

    Xrm.WebApi.retrieveRecord("systemuser"userId"?$select=userlimit").then(
        function success(result) {
            var userLimitresult.userlimit;
            if (enteredAmount > userLimit) {
                var alertStrings = { confirmButtonLabel: "OK"text: "The amount exceeds your user limit."title: "Limit Exceeded" };
                Xrm.Navigation.openAlertDialog(alertStrings);
                // Prevent the Save
                executionContext.getEventArgs().preventDefault();
            }
        },
        function (error) {
            var alertStrings = { confirmButtonLabel: "OK"text: "Error retrieving system user limit: " + error.messagetitle: "Error Encountered" };
            Xrm.Navigation.openAlertDialog(alertStrings);
        }
    );
}
I have the same question (0)
  • magesoe Profile Picture
    80 on at

    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

  • Verified answer
    a33ik Profile Picture
    3,306 Most Valuable Professional on at

    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/

  • Verified answer
    Diana Birkelbach Profile Picture
    3,072 Most Valuable Professional on at

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

  • Verified answer
    tschopp Profile Picture
    311 on at

    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.

  • Community Power Platform Member Profile Picture
    on at

    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();
    
     }

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 717 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 329 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard