AX / D365FO – Create a simple RunBase with Dialog through X++

Add a new Runnable class and rename it CustTableDlg:

class CustTableDlg extends RunBase
{
    // User Input fields
    DialogField     fieldAccount;
    DialogField     fieldName;
 
    // Variables to store user input
    CustTable   custTable;
    CustAccount custAccount;
 
    // pack() and unpack() methods are used to load the last value from user
    // for our simple example we are not going to use them.
    public container pack()
    {
        return conNull();
    }
 
    public boolean unpack(container packedClass)
    {
        return true;
    }
}

Create a dialog method to capture runtime user inputs for customer details:

// Dialog method to capture runtime user inputs for customer details
public Object dialog()
{
    Dialog dialog = super();
 
    // Set a title for dialog
    dialog.caption( 'Simple Dialog');
 
    // Add a new field to Dialog
    fieldAccount = dialog.addField(extendedTypeStr(CustAccount), 'Customer account');
 
    return dialog;
}

Override the method getFromDialog, the code below will be used to retrieve the Dialog field values:

// Retrieve values from Dialog
public boolean getFromDialog()
{
    custAccount = fieldAccount.value();
    return super();
}

Override the method run, use it to process whatever you want to. On my example I will use it to show the customer account information on infolog.

public void run()
{
    // Set Dialog field value to find CustTable
    custTable = CustTable::find(custAccount);
 
    if (custTable)
    {
        info(strFmt('%1 -- %2', custTable.AccountNum, custTable.name()));
    }
    else
    {
        error( 'Customer Account not found!');
    }
}

Create a new main method to execute your class and build it:

public static void main(Args _args)
{
    CustCreateDialog custCreate = new CustCreateDialog();
 
    // Prompt the dialog, if user clicks in OK it returns true
    if (custCreate.prompt())
    {
        custCreate.run();
    }
}

Here you can find the full snippet:

class CustTableDlg extends RunBase
{
    // User Input fields
    DialogField     fieldAccount;
    DialogField     fieldName;
 
    // Variables to store user input
    CustTable   custTable;
    CustAccount custAccount;
 
    // pack() and unpack() methods are used to load the last value from user
    // for our simple example we are not going to use them.
    public container pack()
    {
        return conNull();
    }
 
    public boolean unpack(container packedClass)
    {
        return true;
    }
 
    // Dialog method to capture runtime user inputs for customer details
    public Object dialog()
    {
        Dialog dialog = super();
 
        // Set a title for dialog
        dialog.caption( 'Simple Dialog');
 
        // Add a new field to Dialog
        fieldAccount = dialog.addField(extendedTypeStr(CustAccount), 'Customer account');
 
        return dialog;
    }
 
    // Retrieve values from Dialog
    public boolean getFromDialog()
    {
        custAccount = fieldAccount.value();
        return super();
    }
 
    public void run()
    {
        // Set Dialog field value to find CustTable
        custTable = CustTable::find(custAccount);
 
        if (custTable)
        {
            info(strFmt('%1 -- %2', custTable.AccountNum, custTable.name()));
        }
        else
        {
            error( 'Customer Account not found!');
        }
    }
 
    public static void main(Args _args)
    {
        CustTableDlg custTableDlg = new CustTableDlg();
 
        // Prompt the dialog, if user clicks OK it returns true
        if (custTableDlg.prompt())
        {
            custTableDlg.run();
        }
    }
 
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s