The best practice for confirmation dialog box in Dynamics 365 Business Central (Using Codeunit 27 “Confirm Management”)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about the best practice for Confirm dialog box in Dynamics 365 Business Central.

As you might know, you can use Dialog.Confirm Method to create a dialog box that prompts the user for a yes or no answer. The dialog box is centered on the screen. This is very easy.

Dialog.Confirm Method:
Syntax: Ok := Dialog.Confirm(String: String [, Default: Boolean] [, Value1: Any,…])

Parameters:

String
 Type: String
Specifies the string that is displayed in the dialog box. Use a backslash (\) to indicate a new line. The string can be a text constant that is enabled for multilanguage functionality.

[Optional] Default
 Type: Boolean
Specifies the default button. If you do not specify a default button, then No is used as the default button.
[Optional] Value1
 Type: Any

Return Value: Ok
 Type: Boolean

For example:

Confirm Message.

Select Yes.

Select No.

If your program is only used in this one place, there is usually no problem, but you need to consider if there is a Codeunit, Job Queue, API, etc. that will call your code. You have to think about how the Confirm dialog box should behave with no user input, otherwise, the process will be interrupted.

For example,

You can create new customers on the page (UI).

But if you set this codeunit in Job Queue.

You will get an error.

Microsoft Dynamics 365 Business Central Server attempted to issue a client callback to show a confirmation dialog box: Do you want to create a new customer? (CodeUnit 50100 CreateNewCustomer). Client callbacks are not supported on Microsoft Dynamics 365

In this case, I think you may generally use the System.GuiAllowed Method.

System.GuiAllowed Method: Checks whether the AL code can show any information on the screen.

Yes, this can solve the problem. But please note that this is not the best way.

In System Application, you can find the following two codeunits.
For more details: Confirm Management Module.

codeunit 27 “Confirm Management”

codeunit 26 “Confirm Management Impl.” (Access = Internal;)

There are two methods in codeunit 27 “Confirm Management”.

procedure GetResponseOrDefault:
Raises a confirm dialog with a question and the default response on which the cursor is shown. If UI is not allowed, the default response is returned.

procedure GetResponse: Always returns false
Raises a confirm dialog with a question and the default response on which the cursor is shown. If UI is not allowed, the function returns FALSE.

So you can easily change your code like below.

codeunit 50100 CreateNewCustomer
{
    trigger OnRun()
    var
        Cust: Record Customer;
        ConfirmManagement: Codeunit "Confirm Management";
    begin
        /*if not GuiAllowed then
            exit;
        if not Confirm('Do you want to create a new customer?', true) then
            exit;*/
        if not ConfirmManagement.GetResponseOrDefault('Do you want to create a new customer?', true) then
            exit;
        Cust.Init();
        Cust.Validate(Name, 'Customer Name');
        Cust.Insert(true);
    end;
}

PS: This method has been used by Microsoft in several standard features.

END

Hope this will help.

Thanks for reading.

ZHU

コメント

タイトルとURLをコピーしました