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 :
Microsoft Dynamics CRM (Archived)

Intercepting save with a custom prompt

(0) ShareShare
ReportReport
Posted on by 3,079

We have a custom entity where we need to check, on the attempt to save a record, if a particular set of conditions is true (querying other records, etc...).  If it isn't, it should just save normally.  If it is, then we want a prompt to come up, giving them some information, and confirming whether they want to save or cancel.

I've got a function triggering onSave, and it's checking my conditions without problem.  I have alert.js loaded to bring up the prompt, and have the buttons, the functions, etc...  However, if I bring up a prompt through alert.js, it's still going ahead and saving the record in the background, before I can answer the prompt.  I could put a wait timer or something, but then if they take longer than that it will still happen, etc...

I thought perhaps I could cancel the save at the start of the function with getEventArgs().preventDefault(), do the checks, and then use xrm.page.data.save() to go ahead with the save only when they say to do (or when the conditions are not met).  I worried that this might loop, but figured I'd test it.  It's definitely not looping, because the save is failing (i.e. it's hitting the failure callback function).  But I'm getting no messaging in CRM about why the save would fail, and if I take out the preventDefault call, the record is able to be saved with no difficulty.  So I'm guessing perhaps the preventDefault call is setting some sort of flag that's keeping the record from saving even after I manually call the save again.

Any way to clear that flag (if it exists), or any other way to go about this problem?

Thanks!

*This post is locked for comments

I have the same question (0)
  • ashlega Profile Picture
    34,477 on at

    Hi Allison,

     I would try making that xrm.page.data.save() call from a timer function to clean up the "flag":

     setTimeout(function(){Xrm.Page.data.save();}, 10);

     Not sure what exactly you are doing, but it might be all about this:

    "Returns a value indicating whether the save event has been canceled because the preventDefault method was used in this event hander or a previous event handler."

    https://msdn.microsoft.com/en-us/library/gg509060.aspx#BKMK_preventDefault

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    I think this approach would work. Add a bit variable (global) to the form called canSave and set it to false. Before calling the preventDefault check the value of that canSave, and only call the preventDefault if canSave = false.

    When you manually call the save event, change the can Save to true, so that the preventDefault statement does not execute.

    After the form is saved and on the onLoad event, set the value of canSave to false again.

    Hope this helps.

  • awalters Profile Picture
    3,079 on at

    Alex - that was my guess as well.  I wasn't 100% sure that would prevent a future save, but it seems to be the case.  I did try the line you suggested, but the save is still failing.  :-(  Thanks though!

  • ashlega Profile Picture
    34,477 on at

    Any chance you could post your code?

  • awalters Profile Picture
    3,079 on at

    Aric: bloody hell - I see what you're saying.  That will likely work.  Shall give it a try and report back.  This all feels really inelegant (my bits included), but I haven't figured out any other way to insert this yes/no check.  :-)  Thanks!

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Can't hurt to try it. Not the cleanest approach, but if it works, great...

  • awalters Profile Picture
    3,079 on at

    Hmmm...giving it a try, and I can't figure out how to get the timing to work.  The timing issue is actually exactly the same, as far as I can see.  The place where I need to set the flag is in after the button press on the alert popup (when they say yes it's okay, I need to allow it to save).  But the save is already running in the background before that - which is why I was trying to cancel it in the first place...

  • awalters Profile Picture
    3,079 on at

    Alex:  As a test, I'm just trying to prevent the save, then re-initiate the save (without anything in between except an alert).

      ExecutionObj.getEventArgs().preventDefault();
      alert ("hi");
      setTimeout(function(){xrm.page.data.save();}, 10);


    It gets into setTimeout, as it'll do an alert I put in the function but before the save.  But if I put one after the save it doesn't reach it.  (And it's not being saved.)

  • ashlega Profile Picture
    34,477 on at

    you'd have to do it this way, though:

    function onSave(ExecutionObj)

    {

     if(CONDITIONS NOT MET)

     {

        ExecutionObj.getEventArgs().preventDefault();

        alert ("hi");

        return;

     }

    }

    But I'm assuming you have some kind of confirmation message/dialog there.. how are you doing that?

  • ashlega Profile Picture
    34,477 on at

    Or you might do this for the test:

    function onSave(ExecutionObj)

    {

    if(CONDITIONS NOT MET)

    {

       ExecutionObj.getEventArgs().preventDefault();

       alert ("hi");

       setTimeout(function(){Xrm.Page.data.save();}, 10);

       return;

    }

    }

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

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans