web
You’re offline. This is a read only version of the page.
close
Skip to main content
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

I have the same question (0)
  • Suggested answer
    Clem Profile Picture
    2,541 on at
    RE: openConfirmDialog not preventing save

    Hi,

    In order to prevent the save action, you need to pass the executionContext to the SaveMethod like : OnSave(context)

    and then inside the OnSave function use the following to prevent the save action : 

    context.getEventArgs().preventDefault();

    You can try this, but i don't know if the dialog with "freeze" the save process during the display of it.

    Clément

  • RaviKashyap Profile Picture
    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.

  • Clem Profile Picture
    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
    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.

  • Verified answer
    Ben Thompson Profile Picture
    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); }); } } }
  • Verified answer
    Clem Profile Picture
    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;
            
    }


  • RaviKashyap Profile Picture
    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.

  • Swaroop Deshmukh Profile Picture
    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

  • Sumit Desai Profile Picture
    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.

  • Mona Chavan Profile Picture
    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?

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

Responsible AI policies

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

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans