Monday, April 12, 2021

Register override lookup in D365fo from form datasource

/// <summary>
/// Extension of the form 'SalesTable' data source 'SalesLine'.
/// </summary>
[ExtensionOf(formDataSourceStr(SalesTable, SalesLine))]
final class Sales_SalesTableForm_SalesLineDS_Extension
{
    /// <summary>
    /// COC construct to handle standard datasource init method
    /// </summary>
    /// <remarks>
    /// Method overriden to add custom logic
    /// </remarks>
    void init()
    {
        next init();

        this.m_Init();
    }

    /// <summary>
    /// COC Implementation for additional logic on init method
    /// </summary>
    void m_Init()
    {
        this.sm_ReasonCodeLookupRegisterOverride();
    }

    /// <summary>
    /// SM reason code lookup override method
    /// </summary>
    public void m_ReasonCodeLookupRegisterOverride()
    {
        FormDataSource salesLineDataSource = element.dataSource(formDataSourceStr(SalesTable, SalesLine)) as FormDataSource;
        SM_Sales_SalesTableFormRegisterMethods formRegisterMethods = new SM_Sales_SalesTableFormRegisterMethods();

        salesLineDataSource.object(fieldNum(SalesLine, SM_ReasonCode)).registerOverrideMethod(methodStr(FormDataObject, lookup),
                                methodStr(M_Sales_SalesTableFormRegisterMethods, lookupReasoncode ),
                                formRegisterMethods);
              
    }

}

/// <summary>
/// Form 'SalesTable', data source 'SalesLine' register override method implementation
/// </summary>
class Sales_SalesTableFormRegisterMethods
{
    /// <summary>
    /// Overrides <c>SM_Reasoncode</c> field lookup
    /// </summary>
    /// <param name = "_formStringControl">The control that is bound to the <c>SM_Reasoncode</c> field.</param>
    public void lookupReasoncode(FormStringControl _formStringControl)
    {
        FormRun formRun = _formStringControl.formRun();

        SalesTable salesTableLoc = formRun.dataSource(formDataSourceStr(SalesTable, SalesTable)).cursor() as SalesTable;

        SalesLine::m_lookupReasoncode(_formStringControl, salesTableLoc.DlvReason);
    }

}

/// <summary>
    /// Overrides <c>SM_Reasoncode</c> field lookup
    /// </summary>
    /// <param name = "_formStringControl">The control that is bound to the <c>SM_Reasoncode</c> field.</param>
    public static void lookupReasoncode(FormStringControl _formStringControl, DlvReasonId _dlvReason)
    {
        Query query = new Query();

        QueryBuildDataSource queryBuildDataSource = query.addDataSource(tableNum(ReasonTable));
        queryBuildDataSource.addSelectionField(fieldnum(ReasonTable, Reason));

        if (SM_CustParameters::find().ActivateReasonCode)
        {
            container conReasoncode;
            ReasonTable reasonTable;
            SM_DeliveryReasonReasonCodeCombination deliveryReasonReasonCodeCombination;

            while select ReasonCode from deliveryReasonReasonCodeCombination
                exists join reasonTable
                where reasonTable.Reason == deliveryReasonReasonCodeCombination.ReasonCode
                && deliveryReasonReasonCodeCombination.DlvReasonId == _dlvReason
                && reasonTable.SM_ActivateReasonCode == NoYes::Yes
            {
                conReasoncode += deliveryReasonReasonCodeCombination.ReasonCode;
            }

            str reasoncodes = conReasoncode != conNull() ? con2Str(conReasoncode) : SysQuery::valueEmptyString();

            queryBuildDataSource.addRange(fieldnum(ReasonTable, Reason)).value(reasoncodes);
        }
       
        SysTableLookup lookup = SysTableLookup::newParameters(tablenum(ReasonTable), _formStringControl);

        lookup.addLookupfield(fieldnum(ReasonTable, Reason));
        lookup.addLookupfield(fieldnum(ReasonTable, Description));

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

No comments:

Post a Comment

Table browser URL in D365FO

Critical Thinking icon icon by Icons8