Hi Guys,
This is a very common requirement to create Journal from code(X++) but in D365fno some dimension classes are eliminated so here is the code to create and post a customer payment journal in D365 FnO.
1. Create journal header from click method then call below method to create journal lines.
public void clicked()
{
LedgerJournalTable ledgerJournalTable;
CustomerCreditTable customerCreditTable;
CreateJournal createJournal;
LedgerjournalCheckPost LedgerjournalCheckPost;
super();
try
{
ttsbegin;
ledgerJournalTable.initValue();
ledgerJournalTable.initFromLedgerJournalName("@Ext:CustPay");
ledgerJournalTable.JournalNum = JournalTableData::newTable(ledgerJournalTable).nextJournalId();
ledgerJournalTable.Name = strFmt("@Ext:WriteOff",DateTimeUtil::getToday(DateTimeUtil::getUserPreferredTimeZone()));
createJournal = new CreateJournal();
int numlines = createJournal.createJournalLines(ledgerJournalTable.JournalNum,customerCreditTable.JournalNum);
if(numlines > 0)
{
ledgerJournalTable.insert();
if (ledgerJournalTable)
{
LedgerjournalCheckPost = LedgerjournalCheckPost::newLedgerJournalTable(ledgerJournalTable,NoYes::Yes);
LedgerjournalCheckPost.runOperation();
}
ttsbegin;
select firstonly forupdate customerCreditTable
where customerCreditTable.JournalNum == customerCreditTable.JournalNum &&
customerCreditTable.IsProcessed == NoYes::No;
if (customerCreditTable)
{
customerCreditTable.IsProcessed = NoYes::Yes;
customerCreditTable.JournalNumRef = ledgerJournalTable.JournalNum;
customerCreditTable.doUpdate();
}
customerCreditTable_ds.research();
customerCreditTable_ds.refresh();
ttscommit;
info(strFmt("@Ext:JVCreated",ledgerJournalTable.JournalNum, numlines));
}
ttscommit;
}
catch (Exception::Error)
{
throw Exception::Error;
}
}
2. Call this method in the clicked method to create journal lines as per requirement.
public int createJournalLines(LedgerJournalId journalId,LedgerJournalId journalIdOrig)
{
LedgerJournalTrans transJournal;
RecordInsertList ledgerJournalTransList;
NumberSeq numberseq;
Voucher voucherNum;
int counter;
CustomerCreditTable customerCreditTable;
//CustInvoiceJour custInvoiceJour;
//SalesLine salesLine;
//LedgerDimensionDefaultAccount getReasonDim;
LedgerJournalName ledgerJournalName = ledgerJournalName::find("@Ext:CustPay");
ledgerJournalTransList = new RecordInsertList(transJournal.TableId);
numberseq = NumberSeq::newGetVoucherFromCode(NumberSequenceTable::find(ledgerJournalName.NumberSequenceTable).NumberSequence);
voucherNum = numberseq.voucher();
while select customerCreditTable where customerCreditTable.IsProcessed == NoYes::No && customerCreditTable.JournalNum == journalIdOrig
{
transJournal.clear();
transJournal.initValue();
transJournal.JournalNum = journalId;
transJournal.TransDate = DateTimeUtil::getToday(DateTimeUtil::getUserPreferredTimeZone());
transJournal.AccountType = LedgerJournalACType::Cust;
transJournal.LedgerDimension = LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber(customerCreditTable.Customer,LedgerJournalACType::Cust);//Journal_Tmp.LedgerDimension;
transJournal.DefaultDimension = LedgerDimensionFacade::getDefaultDimensionFromLedgerDimension(transJournal.LedgerDimension);
transJournal.OffsetAccountType = customerCreditTable.AccountType;//LedgerJournalACType::Ledger;
//select firstonly InvoiceId,SalesId from custInvoiceJour
// where custInvoiceJour.InvoiceId == customerCreditTable.InvoiceId
// join firstonly DefaultDimension from salesLine
// where salesLine.SalesId == custInvoiceJour.SalesId;
//getReasonDim = CustWriteOffFinancialReasonsSetup::findByReasonCode(customerCreditTable.Reason).WriteOffLedgerDimension;
//transJournal.OffsetLedgerDimension = LedgerDimensionFacade::ServiceCreateLedgerDimension(getReasonDim, salesLine.DefaultDimension);//22565422738);
transJournal.OffsetLedgerDimension = customerCreditTable.LedgerDimension;
transJournal.CurrencyCode = customerCreditTable.Currency;
transJournal.AmountCurCredit = customerCreditTable.Credit;
transJournal.MarkedInvoice = customerCreditTable.InvoiceId;
transJournal.Txt = customerCreditTable.TransactonTxt;
transJournal.Approved = NoYes::Yes;
transJournal.Approver = HcmWorker::userId2Worker(curUserId());
transJournal.SkipBlockedForManualEntryCheck = true;
transJournal.defaultRow();
if(!transJournal.Voucher)
{
transJournal.Voucher = voucherNum;
}
ledgerJournalTransList.add(transJournal);
counter++;
}
ledgerJournalTransList.insertDatabase();
numberseq.used();
return counter;
}
Thanks,
No comments:
Post a Comment