Saturday, 10 June 2017

Extension of Class in D365 for Operations


We can create extension for class but can use/call only Public Members/Methods. Private and Protected can not be accessed from Extension Class.

Added new field TstField in MainAccount Table using extension.
Would like to show newly added field on Trail balance Form/Report.
Added new field TstField in LedgerTrialBalanceTmp Table using extension.

/// <summary>

/// Extension Class for LedgerTrialBalanceDP
/// </summary>
[ExtensionOf(classStr(LedgerTrialBalanceDP))]
final class HNWLedgerTrialBalanceDP_Extension
{
    protected void new()
    {

    }

    /// <summary>
    /// Updating new field in <c>LedgerTrialBalanceTmp</c> Table
    /// </summary>
    public void UpdateTstField()
    {
        MainAccount                         mainAccount;
        DimensionAttributeValueCombination  ledgerDimension;
        LedgerTrialBalanceTmp               ledgerTrialBalanceTmp;
       
        ledgerTrialBalanceTmp = this.getLedgerTrialBalanceTmp();     

        update_recordset ledgerTrialBalanceTmp
            setting
            TstField     =   mainAccount.TstField
            join mainAccount
        exists join ledgerDimension where
            ledgerDimension.MainAccount == mainAccount.RecId &&
            ledgerDimension.RecId == ledgerTrialBalanceTmp.LedgerDimension;

        this.setTrialBalanceTmpTable(ledgerTrialBalanceTmp);
    }

}

Create postEventHandler for processReport method in LedgerTrialBalanceDP Class
and call our newly created method in Extension Class of LedgerTrialBalanceDP


public class HNWLedgerTrailBalanceHandlersExt

{
    [PostHandlerFor(classStr(LedgerTrialBalanceDP), methodStr(LedgerTrialBalanceDP, processReport))]

    public static void LedgerTrialBalanceDP_Post_processReport(XppPrePostArgs args)        {
       LedgerTrialBalanceDP ledgerTrialBalanceDP = args.getThis();
        ledgerTrialBalanceDP.UpdateTstField();
     }
}