Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

openConfirmDialog not preventing save

(0) ShareShare
ReportReport
Posted on by 55,410 Moderator

Hi,

I have implemented openConfirmDialog on the save event. Though it is showing the popup with all the values but it is not preventing save i.e. on click of save, popup appears for confirmation but the form saves even before I select anything on the confirm dialog.

Auto save is not enabled.

 Xrm.Utility.confirmDialog works fine.

Does any one else faced this issue?

https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/xrm-navigation/openconfirmdialog

Here is my code-

----------

 function OnSave() {
        var isCreateChildDirty = Xrm.Page.data.entity.attributes.get("new_createchildcases").getIsDirty();
        var isCreateChild = Xrm.Page.data.entity.attributes.get("new_createchildcases").getValue();
        if (isCreateChildDirty == true && isCreateChild == true) {
            var confirmStrings = { text: "Are you sure you want to create child records for the current total count?", title: "Create Child Cases", subtitle: "Create child cases for the the current total count", cancelButtonLabel: "Cancel", confirmButtonLabel: "Confirm" };
            var confirmOptions = { height: 200, width: 500 };
            Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(function (success) {
                if (success.confirmed)
                    Xrm.Page.getControl("new_createchildcases").setDisabled(true);
                else
                    Xrm.Page.getAttribute("new__createchildcases").setValue(false);
            });
        }
    }

---------------

*This post is locked for comments

  • Suggested answer
    stjanovitz Profile Picture
    stjanovitz on at
    RE: openConfirmDialog not preventing save

    With the new asynchronous event handler support for the form OnSave event, you can return the Promise returned by openConfirmDialog to pause the save. You can then use the preventDefault method to stop the save as appropriate.

  • Mona Chavan Profile Picture
    Mona Chavan 255 on at
    RE: openConfirmDialog not preventing save

    Hi Clément Olivier ,

    I have used same code for myself for Confirmation box in my js.

    But its not working for Save and Close button. its saving the record but not closing it.¨

    What can i do for this?

  • Sumit Desai Profile Picture
    Sumit Desai 66 on at
    RE: openConfirmDialog not preventing save

    Yes , This should works and stop save operation synchronously.

    In my case , I'm using editable grid for an associated view and on saving confirmation dialog is coming but in the background save operation is happening without considering the confirmation box output response .

    I tried the above workaround provided but preventDefault itselt not working on editable grid.

  • Swaroop Deshmukh Profile Picture
    Swaroop Deshmukh 280 on at
    RE: openConfirmDialog not preventing save

    Hello,

    Can you guys confirm if this has worked on the new Unified user interface for CRM?

    Thanks,

    Swaroop

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: openConfirmDialog not preventing save

    Thanks Ben & Clement.

    Putting a flag did the trick. However I still believe this is a bug and it should automatically stop the execution if teh call is made to confirmdialog..The way current Xrm.Utility.confirmDialog works.

  • Verified answer
    Clem Profile Picture
    Clem 2,541 on at
    RE: openConfirmDialog not preventing save

    Here is the fix for the infinite loop : 

    var saveFromConfirmDialog = false;
    
    function OnSave(context) {
    
    	if(!saveFromConfirmDialog) {
    		context.getEventArgs().preventDefault();
    			
    		var confirmStrings = { text: "Are you sure you want to create child records for the current total count?", title: "Create Child Cases", subtitle: "Create child cases for the the current total count", cancelButtonLabel: "Cancel", confirmButtonLabel: "Confirm" };
    		var confirmOptions = { height: 200, width: 500 };
    		Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(function (success) {
    			if (success.confirmed)
    			{
    				saveFromConfirmDialog = true;
    				console.log("saving");
    				Xrm.Page.data.save();
    			}
    				
    			else {
    				console.log("nop");
    				saveFromConfirmDialog = false;
    			}
    		});
    	}
    	saveFromConfirmDialog = false;
            
    }


  • Verified answer
    Ben Thompson Profile Picture
    Ben Thompson 6,350 on at
    RE: openConfirmDialog not preventing save

    There is definitely an infinite loop built into that code (unintentionally) as the second save request will trigger the whole routine again. So you need to add a flag that ensures the second pass through bypasses the logic... Something like this will work but it's not tested and I'm not 100% sure of the .then bit after the save request.

    var saveInProgress=false;
    
    
    function OnSave() {
    if (!saveInProgress){
    saveInProgress=true; context.getEventArgs().preventDefault(); var isCreateChildDirty = Xrm.Page.data.entity.attributes.get("new_createchildcases").getIsDirty(); var isCreateChild = Xrm.Page.data.entity.attributes.get("new_createchildcases").getValue(); if (isCreateChildDirty == true && isCreateChild == true) { var confirmStrings = { text: "Are you sure you want to create child records for the current total count?", title: "Create Child Cases", subtitle: "Create child cases for the the current total count", cancelButtonLabel: "Cancel", confirmButtonLabel: "Confirm" }; var confirmOptions = { height: 200, width: 500 }; Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(function (success) { if (success.confirmed){ Xrm.Page.getControl("new_createchildcases").setDisabled(true); // NOW you save the form Xrm.Page.data.save().then(function(saveInProgress=false;)); } else Xrm.Page.getAttribute("new__createchildcases").setValue(false); }); } } }
  • RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: openConfirmDialog not preventing save

    Hi Clement,

    I thought about that and tried it. It was going into some kind of loop i.e. onsave, calling save, preventing save so nothing was happening. Can you please try this at your end as well? Just wanted to know if this is a bug/ issue or I am missing anything.

  • Clem Profile Picture
    Clem 2,541 on at
    RE: openConfirmDialog not preventing save

    What if you prevent the complete "out of the box" save.
    And based on the result of your dialog, you then call the save function with some JS ?

    Would give something like : 

    function OnSave() {
    	context.getEventArgs().preventDefault();
    	var isCreateChildDirty = Xrm.Page.data.entity.attributes.get("new_createchildcases").getIsDirty();
    	var isCreateChild = Xrm.Page.data.entity.attributes.get("new_createchildcases").getValue();
    	if (isCreateChildDirty == true && isCreateChild == true) {
    		var confirmStrings = { text: "Are you sure you want to create child records for the current total count?", title: "Create Child Cases", subtitle: "Create child cases for the the current total count", cancelButtonLabel: "Cancel", confirmButtonLabel: "Confirm" };
    		var confirmOptions = { height: 200, width: 500 };
    		Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(function (success) {
    			if (success.confirmed){
    				Xrm.Page.getControl("new_createchildcases").setDisabled(true);
    				// NOW you save the form
    				Xrm.Page.data.save();
    			}
    			else
    				Xrm.Page.getAttribute("new__createchildcases").setValue(false);
    		});
    	}
    }


  • RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: openConfirmDialog not preventing save

    Hi Clement,

    I don't want to prevent save completely, it should prevent untill I select Confirm on my confirm dialog box. Di you try to use this new method, i think it's a bug cause the older one XRM.Utility works fine.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,432 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans