Sunday, 6 March 2022

Create general journal using X++ in D365FO

 The general journal can be created using X++ code. This code which I am sharing in this article helps you to create general journal using X++ in D365FO.

In Microsoft Dynamics 365 for Finance and Operations, creating the general journal through X++ is different than creating in Microsoft Dynamics AX 2012. In D365FO, there is an abstract class named as MCRLedgerJournal, which you have to inherit and implement by yourselves.

I have created the MCRLedgerJournal_XYZ class which inherits (extends) the abstract class. To see how to implement the abstract class, you can get the example by viewing MCRLedgerJournal_CustPayment, MCRLedgerJournal_Daily and MCRLedgerJournal_Payment etc. classes.

In our case, I have copied the MCRLedgerJournal_Daily class and pasted it in my own class (MCRLedgerJournal_XYZ) and modifies it according to my requirements. Apart from this, you can also use the MCRLedgerJournal_Daily class directly if you don’t want to implement your own class.

Following is the code to implement the MCRLedgerJournal abstract class.

class MCRLedgerJournal_XYZ extends MCRLedgerJournal
{
    public LedgerJournalTrans createLedgerJournalTrans(AmountCurCredit _amtCurCredit, AmountCurDebit _amtCurDebit, LedgerJournalACType _ledgerJournalACType = LedgerJournalACType::Ledger)
    {
        LedgerJournalTrans ledgerJournalTrans;

        // Validate the input to the ledger journal trans and ensure that a ledgerjournaltable
        // exists before creating the ledger journalTrans.
        if (this.validateInputLedgerJourTrans(_amtCurCredit, _amtCurDebit)
            && ledgerJournalTable.RecId != 0)
        {
            this.initLedgerJournalTrans(ledgerJournalTrans);

            ledgerJournalTrans.JournalNum = ledgerJournalTable.JournalNum;
            ledgerJournalTrans.AccountType = LedgerJournalACType::Ledger;
            ledgerJournalTrans.LedgerDimension = ledgerAccount;
            ledgerJournalTrans.OffsetAccountType = ledgerOffsetAccountType;
            ledgerJournalTrans.OffsetLedgerDimension = ledgerOffsetAccount;
            ledgerJournalTrans.Txt = transTxt;
            ledgerJournalTrans.CurrencyCode = curCode;
            ledgerJournalTrans.ExchRate = exchRate;
            ledgerJournalTrans.Approver = approver;
            ledgerJournalTrans.Approved = approved;
            // Bank reconciliation specific.
            ledgerJournalTrans.MCRCCGeneralLedgerId = MCRCCGeneralLedgerId;
            ledgerJournalTrans.Due = dueDate;
            ledgerJournalTrans.LineNum = lineNum;
            ledgerJournalTrans.AccountType = ledgerAccountType;
            ledgerJournalTrans.Voucher = voucherNum;
            ledgerJournalTrans.PaymReference = paymReference;
            ledgerJournalTrans.BankTransType = bankTransType;
            ledgerJournalTrans.PaymMode = paymentMode;
            // Add the ref paym id and the paym order id to ledger journal trans.
            ledgerJournalTrans.MCRRefPaymID = MCRRefPaymID;
            ledgerJournalTrans.MCRPaymOrderId = MCRPaymOrderID;

            if (_amtCurCredit != 0)
            {
                ledgerJournalTrans.AmountCurCredit = _amtCurCredit;
            }
            if (_amtCurDebit != 0)
            {
                ledgerJournalTrans.AmountCurDebit = _amtCurDebit;
            }
            ledgerJournalTrans.initValue();
            ledgerJournalTrans.defaultRow();

            ledgerJournalTrans.insert();
        }
        else
        {
            throw error("@MCR35845");
        }

        return ledgerJournalTrans;
    }

    /// <summary>
    /// Creates a new instance of the <c>MCRLedgerJournal_Daily</c> class.
    /// </summary>
    /// <param name="_ledgerJournalType">
    /// The type of journal to be used in the instantiation of this class.
    /// </param>
    /// <param name="_journalName">
    /// The name of the journal to be used in the instantiation of this
    /// class; optional.
    /// </param>
    public void new(LedgerJournalType _ledgerJournalType, LedgerJournalNameId _journalName="")
    {
        super(_ledgerJournalType, _journalName);
    }

    protected boolean validateInputLedgerJourTrans(AmountCur _amtCurCredit, AmountCur _amtCurDebit)
    {
        boolean ret;

        ret = super(_amtCurCredit, _amtCurDebit);
        if (ret)
        {
            if (ledgerAccount == 0)
            {
                throw error(strFmt("@MCR36388"));
            }
        }
        return ret;
    }

}

Now, the next step is to create the General journal using the above created class. I have written a job which creates the General journal using X++. See the following code:

class GenerateGeneralJournalJob
{
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {       
        MCRLedgerJournal        journalTable;
        LedgerJournalTable      ledgerJournalTable;
        Counter                 recordsInserted;

        ttsbegin;
        //Creates the journal header table
        journalTable = new MCRLedgerJournal_XYZ(LedgerJournalType::Daily);        
        ledgerJournalTable = journalTable.createLedgerJournalTable();
        journalTable.parmLedgerJournalTable(ledgerJournalTable);
        journalTable.parmMCRCCGeneralLedgerId();
        journalTable.parmCurrencyCode(CompanyInfoHelper::standardCurrency());


        //I am creating two lines to balance out each other.

        // creates a new line in general journal
        journalTable.parmLineNum();
        journalTable.parmLedgerAccountType(LedgerJournalACType::Vend);
        journalTable.parmLedgerAccount(LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber("V-000001",LedgerJournalACType::Vend)); //You need to use the vendor account as per your environment's data.
        journalTable.parmTransDate(today());
        journalTable.parmTransTxt("txt");
        journalTable.createLedgerJournalTrans(abs(0),abs(100),LedgerJournalACType::Vend);

        // creates a new line in general journal
        journalTable.parmLineNum();
        journalTable.parmLedgerAccountType(LedgerJournalACType::Ledger);
        //Using RecId of Ledger Dimension, you need to enter the RecId as per your environment's data.
        journalTable.parmLedgerAccount(5637179073); 
        journalTable.parmTransDate(today());
        journalTable.parmTransTxt("txt");
        journalTable.createLedgerJournalTrans(abs(100),abs(0),LedgerJournalACType::Ledger);
        ttscommit;
    }

}

No comments:

Post a Comment

Clean up jobs in D365FO

  https://anithasantosh.wordpress.com/2024/11/07/clean-up-jobs-in-d365fo/