How to force a confirmation dialog to appear before record save in Dynamics 365

Below is an example of working code which displays a confirmation dialog before the save of a new record, if the user presses ‘OK’ then the record will save or if the user presses ‘Cancel’ then the record will not save. This will only work if you have autosave switched off in the settings Disable auto-save in a model-driven app with Power Apps – Power Apps | Microsoft Learn.

if (typeof CRM === 'undefined') CRM = {};

if (typeof CRM.Account === "undefined") CRM.Account = {};

var count = 0

CRM.Account.OnLoad = function (executionContext) {
    var formContext = executionContext.getFormContext();
};

CRM.Account.OnSave = function (executionContext) {
    var formContext = executionContext.getFormContext();
    CRM.Account.ConfirmationDialog(formContext, executionContext);
};

CRM.Account.ConfirmationDialog = function (formContext, executionContext) {

    var formtype = formContext.ui.getFormType();
    if (count === 0 && formtype === 1) {
        executionContext.getEventArgs().preventDefault();

        var confirmStrings = { text: "Please know that once you press OK the record will be saved", title: "Attention!" };
        var confirmOptions = { height: 200, width: 450 };
        Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
            function (success) {
                if (success.confirmed) {
                    if (count === 0) {
                        count++;
                        formContext.data.entity.save();
                    }
                }
            });
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *