Implementing Confirmation Dialogs in Custom Buttons for Dynamics 365

 

Implementing Confirmation Dialogs in Custom Buttons for Dynamics 365

In Dynamics 365, confirmation dialogs serve as a critical checkpoint, ensuring users consciously acknowledge the actions they're about to take, especially when those actions have significant implications on the data within the system. This article presents a strategy for integrating confirmation dialogs into the workflow of custom buttons created through the Ribbon Workbench.

Introduction

The need for confirmation dialogs is paramount in scenarios where irreversible data modifications are involved. They act as a safeguard, prompting users to verify their intentions, thus mitigating the risk of accidental changes.

The SetActionWithConfirmation Function

Designed for invocation from a custom button in the Ribbon Workbench, the SetActionWithConfirmation function utilizes Dynamics 365's Client API to prompt the user with a confirmation dialog before proceeding with the intended action.

function SetActionWithConfirmation(fieldName, actionValue, primaryControl, confirmationMessage)
{
    if (!primaryControl.getAttribute)
    {
        var errorMessage = 'Custom Ribbon Call must include CRM Parameter = PrimaryControl as third argument.'.';
        var alertStrings = { confirmButtonLabel: "OK", text: errorMessage, title: "Error" };
        Xrm.Navigation.openAlertDialog(alertStrings, null);
        return;
    }

    var proceedWithAction = function()
    {
        ProceedWithAction(fieldName, actionValue, primaryControl);
    };

    if (actionValue != "null")
    {
        if (confirmationMessage)
        {
            var confirmStrings = { text: confirmationMessage, title: "Confirmation" };
            var confirmOptions = { height: 200, width: 450 };
            Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
                function (success)
                {
                    if (success.confirmed)
                    {
                        proceedWithAction();
                    }
                });
        }
        else
        {
            proceedWithAction();
        }
    }
}

function ProceedWithAction(fieldName, actionValue, primaryControl)
{
    var field = primaryControl.getAttribute(fieldName);
    if (field)
    {
        field.setValue(parseInt(actionValue));
        field.setSubmitMode("always");
        field.fireOnChange();
        primaryControl.data.entity.save();
    }
}


Parameters Overview

  • fieldName: The logical name of the field to be affected by the action.
  • actionValue: The value to be applied to the field upon confirmation.
  • primaryControl: The form context, passed as a CRM parameter, representing the primary control for the ribbon button.
  • confirmationMessage: The message displayed within the confirmation dialog, prompting the user for verification.

Understanding field.setSubmitMode("always")

The field.setSubmitMode("always") command instructs Dynamics 365 to always submit the specified field's value to the server when the record is saved, regardless of whether the field value has been modified or not. This is particularly useful in scenarios where it's crucial to ensure that a specific field value is re-evaluated by the server-side logic upon record save, even if the user hasn't made any changes to it in the current session.

Implementing the Function

  1. Custom Button Creation: Within the Ribbon Workbench, define a custom button for the desired entity.
  2. Command Association: Link this button to a command executing the SetActionWithConfirmation function.
  3. Parameter Inclusion: Ensure the PrimaryControl parameter is correctly passed to provide the necessary context.

Handling User Interactions

Utilizing Xrm.Navigation.openConfirmDialog, the function displays a confirmation dialog. User affirmation leads to the application of the actionValue to the specified fieldName, followed by saving these changes. Cancellation by the user halts any modifications.

Conclusion

Embedding confirmation dialogs within the custom button logic not only enriches the user interface but also fortifies the data integrity within Dynamics 365. The SetActionWithConfirmation function, along with the separated ProceedWithAction, offers a streamlined approach to integrating these dialogs, thereby enhancing the overall user experience and system reliability.

No comments:

Post a Comment