Create Payment Journal with X++

static server boolean processCheckPayment(CheckModule _checkModule)
{
LedgerJournalName ledgerJournalName;
LedgerJournalTable ledgerJournalTable;
LedgerJournalTrans ledgerJournalTrans;
LedgerJournalCheckPost ledgerJournalCheckPost;

Common common;
NumberSeq numberseq;
CustTrans custTrans;
CustTransOpen custTransOpen;
Specification specOffsetVoucher;

Parameters parameters = Parameters::find();
CustTable custTable = CustTable::find(_checkModule.CustAccount);
boolean result = false;
InvoiceId invoiceId;
;

while select custTransOpen
where custTransOpen.RefRecIdCheck == _checkModule.RecId &&
custTransOpen.UseRefRecId
{
custTrans = custTransOpen.custTrans();
invoiceId = custTrans.Invoice;

try
{
ttsbegin;

select firstonly ledgerJournalName
where ledgerJournalName.JournalName == parameters.LedgerJournalNameId &&
ledgerJournalName.JournalType == LedgerJournalType::CustPayment;

if(!ledgerJournalName.RecId)
throw error(“@CAM184”);

if(ledgerJournalName)
{
ledgerJournalTable.JournalName = ledgerJournalName.JournalName;
ledgerJournalTable.Name = ledgerJournalName.Name;
ledgerJournalTable.insert();

if(ledgerJournalTable)
{
numberseq = NumberSeq::NewGetVoucherFromCode(ledgerJournalName.VoucherSeries);
ledgerJournalTrans.JournalNum = ledgerJournalTable.JournalNum;
ledgerJournalTrans.Voucher = numberseq.voucher();
ledgerJournalTrans.TransDate = today();
ledgerJournalTrans.AccountType = LedgerJournalACType::Cust;
ledgerJournalTrans.Txt = CustTable::find(ledgerJournalTrans.AccountNum).Name;
ledgerJournalTrans.OffsetAccountType = ledgerJournalName.OffsetAccountType;
ledgerJournalTrans.OffsetAccount = ledgerJournalName.OffsetAccount;
ledgerJournalTrans.Dimension = ledgerJournalName.Dimension;
ledgerJournalTrans.CurrencyCode = ledgerJournalTable.CurrencyCode;
ledgerJournalTrans.ExchRate = ExchRates::find(ledgerJournalTable.CurrencyCode, datenull(), NoYes::No, NoYes::No).ExchRate;
ledgerJournalTrans.TransactionType = LedgerTransType::Payment;
ledgerJournalTrans.PaymMode = parameters.PaymMode;
ledgerJournalTrans.SettleVoucher = SettlementType::SelectedTransact;
ledgerJournalTrans.AccountNum = custTable.AccountNum;
ledgerJournalTrans.AmountCurCredit = custTransOpen.AmountCur;
ledgerJournalTrans.insert();

select custTransOpen
where custTransOpen.AccountNum == custTable.AccountNum &&
custTransOpen.RefRecId == CustTrans::findFromInvoice(invoiceId).RecId;

if(custTransOpen)
{
common = ledgerJournalTrans;
specOffsetVoucher = Specification_OffsetVoucher::newCommon(common, true);
if (!specOffsetVoucher.isMarked(custTransOpen.TableId, custTransOpen.RecId))
if (ledgerJournalTrans.RecId)
specOffsetVoucher.create(custTransOpen.TableId, custTransOpen.RecId, custTransOpen.AmountCur, ledgerJournalTable.CurrencyCode);
}

ledgerJournalCheckPost = LedgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable, NoYes::Yes);
ledgerJournalCheckPost.run();
result = true;
}
}
ttscommit;
}
catch
{
ttsabort;
result = false;
}
}
return result;
}