Thursday 23 June 2011

Handling controls on a dialog in AX

Add the following code in the class declaration

    DialogField     dlgType;
    DialogField     dlgAccount;
    DialogField     dlgName;
    #DEFINE.dialogTypeFieldNo(800)
    #DEFINE.dialogTypeCtrlName("Fld800_1")
    #DEFINE.dialogAccountFieldNo(900)
    #DEFINE.dialogAccountCtrlName("Fld900_1")

To allow the dialog to raise dialog field events
Override the dialogPostRun method and add the following code below the super()

    dialog.dialogForm().formRun().controlMethodOverload(true);
    dialog.dialogForm().formRun().controlMethodOverloadObject(this);

To add the controls to dialog

     dlgType = new DialogField(dialog, typeid(SalesPurch),#dialogTypeFieldNo);
    dlgType.init(dialog);
    dlgType.label("Account Type");

    // assert if resulting field name was not set as expected as this must match the method which processes the event, e.g. fld800_1_validate
    Debug::assert(dlgType.fieldname() == #dialogTypeCtrlName);

    dlgAccount = new DialogField(dialog,typeid(CustAccount),#dialogAccountFieldNo);
    dialog.addCtrlDialogField(dlgAccount.name());
    dlgAccount.init(dialog);
    dlgAccount.label("Account");
    dlgAccount.value();

    // assert if resulting field name was not set as expected as this must match the method which processes the event, e.g. fld900_1_validate
    Debug::assert(dlgAccount.fieldname() == #dialogAccountCtrlName);

    dlgName = dialog.addField(typeId(Name));
    dlgName.allowEdit(false);

Create a new method to handle the modified event of the account type field.
/// <summary>
/// Handling modified event of Account Type Control
/// </summary>
private boolean fld800_1_modified()
{
    Object control;
    boolean valueWasModified;
    ;
    control = dialog.formRun().controlCallingMethod();
    valueWasModified = control.modified();
    if (valueWasModified)
    {
        dlgAccount.value('');
        dlgName.value('');
    }

    return valueWasModified;
}

Create a new method to handle the lookup event of account field on a dialog.
/// <summary>
/// Opens the account lookup based on the account type selected.
/// </summary>
private void fld900_1_lookup()
{
    FormControl formControl=new FormControl();
    LedgerJournalACType accountType;
    ;
    formControl = dlgAccount.control();
    if (dlgType.value() == SalesPurch::Sales)
    {
        CustTable::lookupCustomer(formControl);
    }
    else if (dlgType.value() == SalesPurch::Purch)
    {
        VendTable::lookupVendor(formControl);
    }
}
Create a new method to handle the modified event of account field.
// handle the modified event for the "Account" dialog field
private void fld900_1_modified()
{

    CustTable   custTable;
    VendTable   vendTable;
    Object      control;
    boolean     valueWasModified;
    ;

    control = dialog.formRun().controlCallingMethod();
    valueWasModified = control.modified();

    if(valueWasModified)
    {
        if(dlgAccount.value()!='')
        {
            if (dlgType.value() == SalesPurch::Sales)
            {
                custTable   =   CustTable::find(dlgAccount.value());
                dlgName.value(custTable.Name);
            }
            else if (dlgType.value() == SalesPurch::Purch)
            {
                vendTable   =   VendTable::find(dlgAccount.value());
                dlgName.value(vendTable.Name);
            }
        }
        else
        {
            dlgName.value('');
        }
    }
}

You can also refer the following Std. AX classes.
TransactionReversal_Cust or CustVendCreatePaymJournal

No comments:

Post a Comment