WINDOWS Live Search

To contact me for any clarifications regarding any posts / information, please mail me at arijit [dot] basu [at] gmail [dot] com


Friday, July 27, 2007

Post Ledger Transactions via X++

For beginner AX developers, I’m listing some codes which will help someone to post ledger transactions via X++ codes. This may be helpful to new Developers. [Level 200]

There are two ways to be considered when posting transactions:
1. Use the LedgerVoucher class and sub-classes API.
2. Use a journal Classes.

1. Use the LedgerVoucher class and sub-classes API.

The Classes are:
• LedgerVoucher - Posting {LedgerVoucher harnesses the posting of multiple vouchers at a time. }
• LedgerVoucherObject - Voucher {The LedgerVoucher class holds all the vouchers in temporary storage.}
• LedgerVoucherTransObject – Transactions {Holds transactions in each Voucher}

Instantiation of LedgerVoucher

LedgerVoucher::newLedgerPost(_detailSummary,
_sysModule,
_voucherSeriesCode,
[_transactionLogType,
_transactionLogText,
_approveJournal,
_posting]);

Instantiation of LedgerVoucherObject

LedgerVoucherObject::newVoucher(_voucher,
[_transDate,
_sysModule,
_ledgerTransType,
_correction,
_operationsTax,
_documentNum,
_documentDate]);

Instantiation of LedgerVoucherTransObject

LedgerVoucherTransObject newCreateTrans(_ledgerVoucherObject,
_ledgerPostingType,
_ledgerAccount,
_dimension,
_currencyCode,
_amountCur,
_sourceTableId,
_sourceRecId,
[_qty,
_exchRate ,
_exchRateSecond,
_exchRatesTriangulation,
_markBridging ,
ProjLedger,
AmountMST ])

Sample Code
static void LedgerVoucherAPISample(Args _args)
{
LedgerVoucher _LedgerVoucher;
LedgerVoucherObject _LedgerVoucherObject;
LedgerVoucherTransObject _LedgerVoucherTransObject;
NumberSeq _NumberSeq;
Dimension _Dimension;

NumberSequenceCode _VoucherCode = 'Ledger_3'; // 'Ledger_3' is the voucher Numbersequence in the Path AX>General Ledger>Setup>Journals>Journal Names. Its the Voucher Numref for the Journal Name :)
LedgerAccount _Account = '110180';
LedgerAccount _OffsetAccount = '140270';
AmountCur _AmountCur = 12345.67;

ttsbegin;
_NumberSeq = NumberSeq::newGetVoucherFromCode(_VoucherCode);
_LedgerVoucher = LedgerVoucher::newLedgerPost(DetailSummary::Detail,
SysModule::Ledger,
_VoucherCode);
_LedgerVoucherObject = LedgerVoucherObject::newVoucher(_NumberSeq.voucher());
_LedgerVoucher.addVoucher(_LedgerVoucherObject);
_LedgerVoucherTransObject = LedgerVoucherTransObject::newCreateTrans(_LedgerVoucherObject,
LedgerPostingType::LedgerJournal,
_Account,
_Dimension,
CompanyInfo::standardCurrency(),
_AmountCur,
0,
0);
_LedgerVoucherTransObject.parmTransTxt("Arijit Basu");
_LedgerVoucher.addTrans(_LedgerVoucherTransObject);
_LedgerVoucherTransObject = LedgerVoucherTransObject::newCreateTrans(_LedgerVoucherObject,
LedgerPostingType::LedgerJournal,
_OffsetAccount,
_Dimension,
CompanyInfo::standardCurrency(),
_AmountCur*-1,
0,
0);
_LedgerVoucherTransObject.parmTransTxt("Arijit Basu");
_LedgerVoucher.addTrans(_LedgerVoucherTransObject);
_LedgerVoucher.end();

ttscommit;
}

2. Use a journal Classes.

The tables used are LedgerJournalName, LedgerJournalTable, and LedgerJournalTrans.
The steps are:
• Create a journal table record {Table\LedgerJournalTable}
• Create lines for each transaction to be posted {Table\LedgerJournalTrans}.
• Post the journal. {Classes\LedgerJournalCheckPost}

Sample Code

static void LedgerJournalCheckPostDemo(Args _args)
{
LedgerJournalTable _LedgerJournalTable;
LedgerJournalTrans _LedgerJournalTrans;
LedgerJournalCheckPost _LedgerJournalCheckPost;
NumberSeq _NumberSeq;

ttsbegin;
//----Journal Header
_LedgerJournalTable.JournalName = 'Day1';
_LedgerJournalTable.initFromLedgerJournalName();
_LedgerJournalTable.Name = 'Daily Journal';
_LedgerJournalTable.insert();
//----Journal Line
_NumberSeq =NumberSeq::newGetVoucherFromCode(LedgerJournalName::find(_LedgerJournalTable.JournalName).VoucherSeries);
_LedgerJournalTrans.Voucher =_NumberSeq.voucher();
_LedgerJournalTrans.JournalNum =_LedgerJournalTable.JournalNum;
_LedgerJournalTrans.CurrencyCode =CompanyInfo::standardCurrency();
_LedgerJournalTrans.ExchRate =Currency::exchRate(_LedgerJournalTrans.CurrencyCode);
_LedgerJournalTrans.AccountNum ='110180';
_LedgerJournalTrans.AmountCurDebit =1000;
_LedgerJournalTrans.TransDate =Today();
_LedgerJournalTrans.OffsetAccount ='140270';
_LedgerJournalTrans.Txt ='Arijit Basu :)';
_LedgerJournalTrans.insert();
//----Journal Posting
_LedgerJournalCheckPost =LedgerJournalCheckPost::newLedgerJournalTable(_LedgerJournalTable,NoYes::Yes);
_LedgerJournalCheckPost.run();
ttscommit;
Info(StrFmt("Journal %1 is posted",_LedgerJournalTable.JournalNum));
}

Happy DAXing :)

4 comments:

Unknown said...

Regarding...
NumberSequenceCode _VoucherCode = 'Ledger_3';

Where does "Ledger_3" come from?

Anonymous said...

What About if I needed to change a dimension of a transaction that was posted incorrectly? I think I could write a X++ code with voucher as parameter and correct it...but, which way is the best?

Anonymous said...

how can i post the correct tax and "connect" the ledgertrans-record with the taxref/taxtrans-record ?

Anonymous said...

Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now. Keep it up!
And according to this article, I totally agree with your opinion, but only this time! :)