Hi,
I am getting errors while Posting a general Journal against Project. I have created a class which is then creating Journal & posting it.
I am calling this class in OnInsert event Handler of ProjTable.
This is the Class which creating & Posting Journal:
class HSHTCreateJournal { LedgerJournalTable ledgerJournalTable; LedgerJournalTrans ledgerJournalTrans; ProjTable projTable; JournalTableData journalTableData; LedgerJournalName ledgerJournalName; LedgerJournalCheckPost ledgerJournalValidate, ledgerJournalPost; Voucher voucherNum; Amount amount = 10000; public void createProjectJournal(ProjId _projId) { ledgerJournalTable.initvalue(); journalTableData = JournalTableData::newTable(ledgerJournalTable); ledgerJournalTable.JournalType = LedgerJournalType::Cost; ledgerJournalTable.JournalNum = journalTableData.nextJournalId(); ledgerJournalTable.JournalName = @"GLRevenue"; ledgerJournalTable.initFromLedgerJournalName(); ledgerJournalTable.initFromLedgerJournalTable(ledgerJournalTable); ledgerJournalTable.VoucherAllocatedAtPosting = NoYes::Yes; ttsbegin; if (ledgerJournalTable.validateWrite()) { ledgerJournalTable.insert(); Info("Journal Header created"); } ttscommit; Info(strFmt("Journal Num: %1",ledgerJournalTable.JournalNum)); ledgerJournalName = LedgerJournalName::find(ledgerJournalTable.JournalName); voucherNum = NumberSeq::newGetVoucherFromId((ledgerJournalName.NumberSequenceTable)).voucher(); select firstonly projTable where projTable.ProjId == _projId; Info(strFmt("Project Id: %1, project Name : %2",projTable.ProjId,projTable.Name)); if (ledgerJournalTable) { ledgerJournalTrans.clear(); ledgerJournalTrans.initValue(); ledgerJournalTrans.JournalNum = ledgerJournalTable.JournalNum; ledgerJournalTrans.Voucher = voucherNum; ledgerJournalTrans.TransDate = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()); ledgerJournalTrans.Qty = 1; ledgerJournalTrans.AccountType = LedgerJournalACType::Project; ledgerJournalTrans.TransactionType = LedgerTransType::Project; ledgerJournalTrans.LedgerDimension = LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber(projTable.ProjId,LedgerJournalACType::Project); ledgerjournalTrans.OffsetAccountType = LedgerJournalACType::Bank; ledgerJournalTrans.CurrencyCode = @'USD'; ledgerJournalTrans.Txt = strFmt("Expense Journal"); ledgerJournalTrans.Approved = NoYes::Yes; if(amount> 0) { ledgerJournalTrans.AmountCurDebit = amount; } else { ledgerJournalTrans.AmountCurCredit = abs(amount); } ledgerJournalTrans.Company = strUpr(curExt()); ledgerJournalTrans.OffsetCompany = strUpr(curExt()); //Validate and insert the journal line ttsbegin; if (ledgerJournalTrans.validateWrite()) { ledgerJournalTrans.insert(); Info("Journal Lines inserted"); } else { throw Exception::Error; } ttscommit; } ttsbegin; // Posting the journal ledgerJournalValidate = ledgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable,NoYes::No); if (ledgerJournalTable.numOfLines() > 0) { ledgerJournalValidate.runOperation(); if (!ledgerJournalValidate.tableErrorLog()) { ledgerJournalPost = ledgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable,NoYes::Yes); ledgerJournalPost.runOperation(); Info("Journal posted Succesfully"); } else { Error(@"errorLog : %1",ledgerJournalValidate.tableErrorLog()); } } else { Error(@"CannotPostJournal"); } ttscommit; } }
This is the OnInsert Event Handler in which I am creating above class Instance:
class HSHTProjTableHandler { ////// /// /// /// [DataEventHandler(tableStr(ProjTable), DataEventType::Inserted)] public static void ProjTable_onInserted(Common sender, DataEventArgs e) { ProjTable projTable = sender as ProjTable; Info(strFmt("project created successfully with Project Id : %1 & Project InvoiceId : %2 ",projTable.ProjId,projTable.ProjInvoiceProjId)); HSHTCreateJournal createJournal = new HSHTCreateJournal(); if (projTable.Type == ProjType::FixedPrice) { ttsbegin; projTable.Status = ProjStatus::Completed; projTable.update(); ttscommit; createJournal.createProjectJournal(projTable.ProjId); } } }
Errors:
Hi Mohit,
I got the solution, actually I forgot to Initialized LedgerJournalTrans_Project table fields.
I took reference from link below & the link you shared as well.
Thanks,
Harshit
Hi Harshit, CurrencyCode is incorrectly mapped to @'USD', please use "USD" instead in Line#49. I believe you are getting this error during Journal creation, add the missing fields.
Check this old thread and article for reference
community.dynamics.com/.../create-ledger-journal-by-code-x
community.dynamics.com/.../create-ledger-journals-in-d365fo-using-x
André Arnaud de Cal...
292,111
Super User 2025 Season 1
Martin Dráb
230,934
Most Valuable Professional
nmaenpaa
101,156