Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Intercepting save with a custom prompt

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

  • awalters Profile Picture
    awalters 3,079 on at
    RE: Intercepting save with a custom prompt

    I was going to be really choked that I'd missed this page in my searching, until I realised you'd just written it up when you posted here...:-D

    Okay - that seems to work!  Thank you!

    It's definitely the same idea as what I was trying to do originally, but I was just a bit off track with syntax and/or timing in a couple of places.  Also, I didn't realise there was the code to check what type of save - that could be very useful.

  • awalters Profile Picture
    awalters 3,079 on at
    RE: Intercepting save with a custom prompt

    Sorry - I've gotten caught up on a completely unrelated fire in the last couple of days.  Hope to get back to testing this shortly...will report back then.  Thanks!!

  • Verified answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Intercepting save with a custom prompt

    Just FYI - there is a complete script (which is using alert.js) here:

    www.itaintboring.com/.../dynamics-365-implementing-on-save-confirmation-dialog-with-alert-js

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Intercepting save with a custom prompt

    Edited the code - should make more sense in your scenario..

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Intercepting save with a custom prompt

    I just tried this code - worked totally fine(you'd need to add your conditions in both IF-s)

    var isConfirmed = false;
    function onSave(ExecutionObj)
    {
    debugger;
    if(CONDITIONS_NOT_MET && !isConfirmed)
    {

    ExecutionObj.getEventArgs().preventDefault();
    alert("TEST");
    setTimeout(function(){popupCallback();}, 2000);
    return;
    }
    isConfirmed = false;
    }
    function popupCallback()
    {
    if(CONDITIONS_MET) {
    isConfirmed = true;
    Xrm.Page.data.save();
    }
    }

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Intercepting save with a custom prompt

    Which version of the code are you using?

  • awalters Profile Picture
    awalters 3,079 on at
    RE: Intercepting save with a custom prompt

    Okay, I fixed the capitalization on the save call in the timeout function, but the save still isn't happening.  I'm not getting an error, and it's hitting the code (I'm getting a second alert I put inside the timeout function), but the record is definitely not saving.  Interestingly now, though - if I don't put in a flag to check for looping, it does continually loop.  So theoretically onSave is triggering, since that's why it's hitting the code over and over again, but if I leave it the record hasn't actually been created.

    If I do put the flag for looping in, the alerts only happen once.  But again, when I try to leave the record, it hasn't been created/saved.

    If I take this little chunk of code out, the record saves fine, so there shouldn't be anything else stopping it...

    So it still seems like the preventDefault is keeping the save from actually taking effect, even when the event is definitely getting triggered.  It's like the save starts, sees something that says "nope, this was cancelled!" and then stops...

  • awalters Profile Picture
    awalters 3,079 on at
    RE: Intercepting save with a custom prompt

    Yep - that overall logic is what I was trying to do originally (minus the variable to prevent looping, but I just hadn't gotten far enough yet to see if I even needed one), and the save wasn't firing at all.  That's why I pared it down to just the alert("hi"), to try to test the idea of just the cancel and re-initiation of the save, to eliminate any of my other logic being the issue.

    The timeout thing may still work to fix that issue with the case corrected (whoops!  that's what I get for copy/pasting when I'm tired - I did have the case right in my original code, though.  Only introduced the error when I tried adding the timeout code suggested here).  Need to find a few mins to check...hopefully shortly.

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Intercepting save with a custom prompt

    with the async popup, it should probably look more like this (I think it's now very similar to what Aric was referring to):

    var isConfirmed = false;

    function onSave(ExecutionObj)

    {

    if(CONDITIONS NOT MET && !isConfirmed)

    {

       ExecutionObj.getEventArgs().preventDefault();

       <ASYNC POPUP>

    }

    isConfirmed = false;

    }

    function popupCallback()

    {

       if(CONFIRMED_IN_POPUP) {

          isConfirmed = true;

          Xrm.Page.data.save(); 

       }

    }

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Intercepting save with a custom prompt

    Gottcha. Did the solution Alex provided work for you?

    If the timeout doesn't workyou, you could still try and use the other option that I specified above.

    Basically prevent default before calling the Alert.show, and in the callback of the alert, change the value of the canSave and call the Xrm.Page.data.save function.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans