D365F&O - X++ - Create and post Customer payment Journal using X++ code.

 1- Create a new class CreatePaymentJournal.

class CreatePaymentJournal
{
    public void createPaymentJournal()
    {
        System.Exception                    exception;
        LedgerJournalTrans                  journalTrans;
        Ledgerjournalname                   ledgerjournalname;
        NumberSeq                           numberSeq;
        LedgerJournalTable                  journalTable;
        MCROrderParameters                  Parameters = MCROrderParameters::find();
        DimensionAttributeValueCombination  DimAttValCom;
        DimensionDynamicAccount offsetLedgerDim;
        SalesTable salesTable;
        LedgerJournalCheckPost  postCustPaymentJournal;
        boolean result = false;
       
        //Create InvoiceJournal
        try
        {
            //Header Section
           
            select firstonly * from ledgerjournalname
                where ledgerjournalname.JournalName == Parameters.LedgerJournalNameId;

            journalTable.clear();
            ttsbegin;
            journalTable.JournalNum       = JournalTableData::newTable(
                journalTable).nextJournalId();
            ttscommit;
            journalTable.JournalName      = Parameters.LedgerJournalNameId;
            journalTable.initFromLedgerJournalName(journalTable.JournalName);
            journalTable.JournalType      = ledgerjournalname.JournalType;
            journalTable.Name             = this.getJournalName();
            journalTable.insert();

            //line Section
            salesTable = salesTable::find("SO-0001");
            journalTrans.clear();
            LedgerJournalEngine engine;
            engine = LedgerJournalEngine::construct(journalTable.JournalType);
            ttsbegin;
            numberSeq                             =   NumberSeq::newGetVoucherFromId(
                (ledgerjournalname.NumberSequenceTable));
            journalTrans.Voucher                  =   numberSeq.voucher();
            ttscommit;
            journalTrans.initValue();
            journalTrans.initFromLedgerJournalTable_LT(journalTable);
            journalTrans.JournalNum               =   journalTable.JournalNum;
            journalTrans.AccountType              =   LedgerJournalACType::Cust;
            journalTrans.Company                  =   curext();

            journalTrans.TransDate                =   systemDateGet();
            journalTrans.Txt                      =   "Refund - " + salesTable.SalesId;
            journalTrans.CurrencyCode             =   OrderRefundTable.CurrencyCode;
            journalTrans.AmountCurDebit           =   2500;

            journalTrans.DefaultDimension = salesTable.DefaultDimension;
            journalTrans.OffsetDefaultDimension = salesTable.DefaultDimension;
            journalTrans.TaxGroup = SalesLine::find(salesTable.SalesId).TaxGroup;
            journalTrans.TaxItemGroup = SalesLine::find(salesTable.SalesId).TaxItemGroup;

            select firstonly * from DimAttValCom
                where DimAttValCom.DisplayValue == salesTable.InvoiceAccount;
            journalTrans.LedgerDimension = DimAttValCom.RecId;

            journalTrans.PaymentStatus      =       CustVendPaymStatus::None;
            journalTrans.OffsetAccountType =  ledgerjournalname.OffsetAccountType;
            journalTrans.OffsetLedgerDimension = ledgerjournalname.OffsetLedgerDimension;
            journalTrans.insert();
       
           
            // Post customer payment journal
            postCustPaymentJournal = LedgerJournalCheckPost::newLedgerJournalTable(
                journalTable, NoYes::Yes);
            postCustPaymentJournal.run();
            result = true;
        }
        catch
        {            
            result = false;
            exception = CLRInterop::getLastException().GetBaseException();
            Error(exception.get_Message());
        }
    }    
   
}

Comments

Popular posts from this blog

D365F&O - X++ - Best Practice Warning BP Rule: [BPErrorUnknownLabel]: Path:

D365F&O - X++ - How to Enable/Disable button on form based on a condition in D365F&O using Event handlers.

D365FO - X++ - Generate CAR (Customized Analysis Report) report of custom model