21 June 2017

Event Handlers in Dynamics AX 7 and Dynamics AX 365 - D365

If you want to create a new event handler then have to create a class and define.

Example:
How to get formRun or element in the event handler in D365

 [PostHandlerFor(formStr(AssetTrans), formMethodStr(AssetTrans, init))]
    public static void AssetTrans_Post_init(XppPrePostArgs args)
    {
        FormRun                     formRunElement;
        FormDataSource              formDataSource;
        CustomTableFixedAssetProfileTmp    assetProfileTmp;
        QueryBuildDataSource        dataSource;

        formRunElement = args.getThis();

        if( formRunElement.args().callerName() == formStr(HHD_FixedAssetProfile))
        {           
            assetProfileTmp = formRunElement.args().record();// formRun calles the args which is contains the record of caller form
            formDataSource  = formRunElement.dataSource();
            dataSource      = formDataSource.query().dataSourceTable(tableNum(AssetTrans));
            dataSource.addRange(fieldNum(AssetTrans,BookId)).value(assetProfileTmp.BookId);
            dataSource.addRange(fieldNum(AssetTrans,AssetId)).value(assetProfileTmp.AssetId);
        }
    }

class VendTableHandler
{
[PostHandlerFor(tableStr(VendTable), tableMethodStr(VendTable, insert))] // Post even Handler

public static void VendTable_Post_insert(XppPrePostArgs _args)
{
VendTable vendTable = _args.getThis();

_args.setReturnValue(VendTable::exists(vendTable.AccountNum));
}

}

// For Pre event handler
// [PreHandlerFor(tableStr(VendTable), tableMethodStr(VendTable, insert))]

Note: once you created an event handler for a table method. You can see + symbol for the method left side.

Ledger Voucher creation Framework and x++ code to create ledger voucher

 Please click her for MS reference file Below is the out of the box example reference and code. SalesInvoiceJournalPostSubBill_Extension-...