AX 2012 / D365FnO – FORM EVENT HANDLER METHODS IN X++

D365fno-PostImage

This article describes events terminology with examples in X++.

You can use the event design pattern to make your code more modular and reusable. The term event is a metaphor that explains how delegates are used. When something important occurs during a program run, other modules might have to process the occurrence. These important occurrences are known as events

Pre / Post form level event handlers

You need to create a new class to write the form Pre / Post event handler methods.

class PurchTable_Form_EventHandler
{
    [PreHandlerFor(formStr(PurchTable), formMethodStr(PurchTable, init))]
    public static void PurchTable_Pre_init(XppPrePostArgs args)
    {
        FormRun     formRun         = args.getThis();
        FormDesign  formDesign      = formRun.design();

        if(formRun.args().menuItemName() == menuItemDisplayStr(PurchTableListPage)) 
        {
            // Your code 
        }
        else if (formRun.args().menuItemName() == menuItemDisplayStr(PurchTable))
        {
            // Your code
        }
    }

    [PostHandlerFor(formStr(PurchTable), formMethodStr(PurchTable, init))]
    public static void PurchTable_Post_init(XppPrePostArgs args)
    {
        FormRun     formRun         = args.getThis();
        FormDesign  formDesign      = formRun.design();

        if(formRun.args().menuItemName() == menuItemDisplayStr(PurchTableListPage))
        {
            FormControl POState;
            POState = formDesign.controlName(formControlStr(PurchTable, PurchTable_State));
            POState.visible(false);
            Info("MenuItemName: PurchTableListPage");
        }
        else if (formRun.args().menuItemName() == menuItemDisplayStr(PurchTable))
        {
            Info("MenuItemName: PurchTable");
        }
    }

}

Form level event handlers – same will apply for others standard form level events like (OnActivated, OnClosing, etc.)

class PurchTable_Form_EventHandler
{
    [FormEventHandler(formStr(PurchTable), FormEventType::Initialized)]
    public static void PurchTable_OnInitialized(xFormRun sender, FormEventArgs e)
    {
        FormRun         formRun         = sender as FormRun;
        FormDesign      formDesign      = formRun.design();
        FormDataSource  purchTable_ds   = formRun.dataSource("PurchTable");
        PurchTable      purchTable      = purchTable_ds.cursor();
        FormControl     customFields    = formDesign.controlName("POHeaderCustomFields");
        customFields.visible(false);
    }   
}

Form DataSource level event handlers – same will apply for others standard form DataSource level events like (OnInitialized, OnActivated, OnDeleted, etc.)

class PurchTable_Form_EventHandler
{
    [FormDataSourceEventHandler(formDataSourceStr(PurchTable, PurchTable), FormDataSourceEventType::InitValue)]
    public static void PurchTable_OnInitValue(FormDataSource sender, FormDataSourceEventArgs e)
    {
        FormRun         formRun;
        FormDataSource  purchTable_ds;
        PurchTable      purchTable;
        formRun         = sender.formRun();
        purchTable_ds   = formRun.dataSource(formDataSourceStr(PurchTable, PurchTable)) as FormDataSource;
        purchTable      = purchLine_ds.cursor();

        if(purchTable.CustAccount)
            purchTable.CustName = CustTable::find(purchTable.CustAccount).name();
    }
}

Form data field level event handlers – same will apply for others data field level events like (OnValidated, OnValidating, etc.)

class PurchTable_Form_EventHandler
{
    //[FormDataFieldEventHandler(formDataFieldStr(FormName, DataSource, DSDataField), FormDataFieldEventType::Modified)]
    [FormDataFieldEventHandler(formDataFieldStr(PurchTable, PurchTable, CustomBaseEnum), FormDataFieldEventType::Modified)]
    public static void CustomBaseEnum_OnModified(FormDataObject sender, FormDataFieldEventArgs e)
    {
        FormRun         formRun         = sender.datasource().formRun();
        FormDesign      formDesign      = formRun.design();
        //FormDataSource  purchTable_ds = formRun.dataSource(FormDataSourceStr(FormName, DataSource)) as FormDataSource;
        //FormDataSource  purchTable_ds = formRun.dataSource("DataSource")); // We can also pass direct dataSource name as string.
        FormDataSource  purchTable_ds   = formRun.dataSource(FormDataSourceStr(PurchTable, PurchTable)) as FormDataSource;
        //FormRun         formRun_1     = purchTable_ds.formRun(); // we can also get the formRun from dataSource
        PurchTable      purchTable      = purchTable_ds.cursor();
        FormControl     customField     = formDesign.controlName("PurchTable_CustomField");
        CustomField.allowEdit(false);
        if(purchTable.CustomBaseEnum == CustomBaseEnum::Value)
            purchTable.CustomField = "Value";
        else
            purchTable.CustomField = "Value_1";
    }

}

Form control level event handlers – same will apply for others control level events like (OnValidated, OnSelectionChanged, OnLookup, etc.)

class PurchTable_Form_EventHandler
{
    //[FormDataFieldEventHandler(formDataFieldStr(FormName, DataSource, DSDataField), FormDataFieldEventType::Modified)]
    [FormDataFieldEventHandler(formDataFieldStr(PurchTable, PurchTable, CustomBaseEnum), FormDataFieldEventType::Modified)]
    public static void CustomBaseEnum_OnModified(FormDataObject sender, FormDataFieldEventArgs e)
    {
        FormRun         formRun         = sender.datasource().formRun();
        FormDesign      formDesign      = formRun.design();
        //FormDataSource  purchTable_ds = formRun.dataSource(FormDataSourceStr(FormName, DataSource)) as FormDataSource;
        //FormDataSource  purchTable_ds = formRun.dataSource("DataSource")); // We can also pass direct dataSource name as string.
        FormDataSource  purchTable_ds   = formRun.dataSource(FormDataSourceStr(PurchTable, PurchTable)) as FormDataSource;
        //FormRun         formRun_1     = purchTable_ds.formRun(); // we can also get the formRun from dataSource
        PurchTable      purchTable      = purchTable_ds.cursor();
        FormControl     customField     = formDesign.controlName("PurchTable_CustomField");
        CustomField.allowEdit(false);
        if(purchTable.CustomBaseEnum == CustomBaseEnum::Value)
            purchTable.CustomField = "Value";
        else
            purchTable.CustomField = "Value_1";
    }

}
This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.