AX7 / D365 Extensions: Extend the Init Method on a Form

Today’s plan? I would like to extend the init() method of a D365fO form. The goal is to get the selected line from the caller after the newly opened form is initialized.

Form Init Method Extension

Like said before, I would like to get the callers record when opening a new form. In this example I will retrieve the CustAccount when opening the InventNonConformance Form.

AX7 Extension Form Init Overview

Find the form that you want to extend in the AOT, right click it and hit “Open designer”.

AX7 Extension Form Init Open Designer

Now we have two options to extend the init method on the form. Option one is to open the “Events” node and hit “Copy event handler method” on the “OnIntialized” event. OnInitialized is a post event, OnInitializing the opposite, a pre event.

AX7 Extension Form Initialized Copy Handler

Option tow is to open the “Methods” node and copy an event handler for the init() method from here.

AX7 Extension Form Init Copy Handler

I added both options in one class to show you the difference, when it comes to get the caller record. First the “OnInitialized” event, then the post event handler.

class InventNonConformanceTableForm
{
[FormEventHandler(formStr(InventNonConformanceTable), FormEventType::Initialized)]
public static void InventNonConformanceTable_OnInitialized(xFormRun sender, FormEventArgs e)
{
Common common = sender.args().record();
CustTable custTable;
if (common is CustTable)
{
custTable = common;
info("FormEventType::Initialized - Selected CustAccount: " + custTable.AccountNum);
}
}
[PostHandlerFor(formStr(InventNonConformanceTable), formMethodStr(InventNonConformanceTable, init))]
public static void InventNonConformanceTable_Post_init(XppPrePostArgs _args)
{
FormRun fr = _args.getThis();
Common common = fr.args().record();
CustTable custTable;
if (common is CustTable)
{
custTable = common;
info("PostHandlerFor formMethodStr(InventNonConformanceTable, init) - Selected CustAccount: " + custTable.AccountNum);
}
}
}

The difference is minimal, in both cases we will call args().record() from the FormRun object. When using the post handler on the init method, we will have to get this FormRun object first, not a big thing, we have just to call theĀ getThis() method on the provided arguments of the handler. The rest of the code is pretty self explaining.

If you have any questions, feel free to comment below.

Result

If we select a customer and hit “Non conformances” from the “SELL” node on the CustTable form, we will see the following infolog.

AX7 Extension Form Result

You may also like...