Tuesday 17 July 2018

D365 / AX 7 - How to override form control methods

Purpose:
The purpose of this document is to show how to override form control methods without overlaying in Dynamics 365 for Operations

Development:
To demonstrate we are using the HRMCompFixedEmplActionDialog form and going to override the planId_Control lookup method.

First of all, create new event handler class HRMCompFixedEmplActionDialogEventHandler and subscribe to post-event handler of form init.

/// <summary>
/// The <c>HRMCompFixedEmplActionDialogEventHandler</c> class is the event handler class
/// for managing HRMCompFixedEmplActionDialog form events
/// </summary>
class HRMCompFixedEmplActionDialogEventHandler
{     

    /// <summary>
    ///  Post event handler for HRMCompFixedEmplActionDialog
    /// </summary>
    /// <param name="_args">Event args</param>
    [PostHandlerFor(formStr(HRMCompFixedEmplActionDialog), formMethodStr(HRMCompFixedEmplActionDialog, init))]
    public static void HRMCompFixedEmplActionDialog_Post_init(XppPrePostArgs _args)
    {
        FormRun formRun = _args.getThis();

        var methodOverrides = HRMCompFixedEmplActionDialogEventHandler::construct();

        FormStringControl PlanIdctrl = formRun.design().controlName(formControlStr(HRMCompFixedEmplActionDialog, planId_Control));
        PlanIdctrl.registerOverrideMethod(methodStr(FormStringControl, lookup), methodStr(HRMCompFixedEmplActionDialogEventHandler, LookupPlanId ), methodOverrides );
    }

    /// <summary>
    /// Constructs a new instance of <c>HRMCompFixedEmplActionDialogEventHandler</c> class.
    /// </summary>
    /// <returns>
    /// A <c>HRMCompFixedEmplActionDialogEventHandler</c> class.
    /// </returns>
    public static HRMCompFixedEmplActionDialogEventHandler construct()
    {
        return new HRMCompFixedEmplActionDialogEventHandler();
    }

    /// <summary>
    ///    Overrided method for Plan id lookup
    /// </summary>
    /// <param name="_ctrl">
    /// FormStringControl buffer
    /// </param>
    public void LookupPlanId(FormStringControl _ctrl)
    {
        SysTableLookup      sysTableLookup = SysTableLookup::newParameters(tableNum(HRMCompFixedPlanTable), _ctrl);
        Query               query;

        QueryBuildDataSource qbds = query.addDataSource(tablenum(HRMCompFixedPlanTable));

        sysTableLookup.addLookupfield(fieldNum(HRMCompFixedPlanTable, PlanId));
        sysTableLookup.addLookupfield(fieldNum(HRMCompFixedPlanTable, Description));
        sysTableLookup.addLookupfield(fieldNum(HRMCompFixedPlanTable, Type));
        sysTableLookup.addLookupfield(fieldNum(HRMCompFixedPlanTable, CurrencyCode));
        sysTableLookup.addLookupfield(fieldNum(HRMCompFixedPlanTable, PayFrequencyId));

        sysTableLookup.parmQuery(query);
        sysTableLookup.performFormLookup();
    }

}



Using this the approach we can override any form of control methods

No comments:

Post a Comment

Difference Between Edit And Display Method in Ax

Display Method: The display method means that the method’s return value is being displayed on a form or a report.  This value is fixed and c...