Thursday, June 15, 2023

Code to get opening balance and closing balance for Ledger Main account in D365

Code to get Opening balance for  MainAccount :

2017 year opening balance will be the closing balance of year 2016.

    public static void main(Args _args)

    {

        date dateTime,datetimeloc,fromdate,todate;

        fromdate = mkDate(01,01,2017);

        todate = mkDate(31,12,2017);

        LedgerBalanceMainAccountAmounts ledgerBalance,ledgerBalanceloc;

        AmountMst  opSum,closingbalance;

        dateTime = any2Date(DateTimeUtil::addYears(fromdate , -1));

        datetimeloc = any2Date(DateTimeUtil::addYears(todate , -1));

        ledgerBalance = LedgerBalanceMainAccountAmounts::construct();

        ledgerBalance.parmIncludeRegularPeriod(true);

        ledgerBalance.parmIncludeOpeningPeriod(true);

        ledgerBalance.parmIncludeClosingPeriod(false);

        ledgerBalance.parmAccountingDateRange(dateTime, datetimeloc);

        ledgerBalance.calculateBalance(MainAccount::findByMainAccountId('202300'));

        opSum = ledgerBalance.getAccountingCurrencyBalance();


        ledgerBalanceloc = LedgerBalanceMainAccountAmounts::construct();

        ledgerBalanceloc.parmIncludeRegularPeriod(true);

        ledgerBalanceloc.parmIncludeOpeningPeriod(true);

        ledgerBalanceloc.parmIncludeClosingPeriod(false);

        ledgerBalanceloc.parmAccountingDateRange(fromdate, todate);

        ledgerBalanceloc.calculateBalance(MainAccount::findByMainAccountId('202300'));

        closingbalance = ledgerBalanceloc.getAccountingCurrencyBalance();

        info(strFmt("Opening balance : %1",opSum));

        info(strfmt("Closing balance : %1", closingbalance));

     }




Reference blog : https://msdynamicsxx2012.blogspot.com/2018/02/code-to-get-opening-balance-for-ledger.html







No comments:

Post a Comment