Posting journals from X++ code on the user's behalf
is an inseparable supplement to journal creation explained in the
Creating new general journals recipe. Normally, this operation is done
right after the journal is created or later when some user action is
triggered.
In this recipe, we will
explore how to post general journals from X++ code. The journal we are
going to process is the one we created in the Creating new general
journals recipe.
How to do it...
1. Open AOT and create a new class called JournalPost with the following code (replace 000152_010 with your journal number):
class JournalPost
{
}
public static void main(Args _args)
{
LedgerJournalCheckPost jourPost;
LedgerJournalTable jourTable;
;
jourTable = LedgerJournalTable::find('000152_010');
jourPost = LedgerJournalCheckPost::newLedgerJournalTable(
jourTable,
NoYes::Yes);
jourPost.run();
}
2. Run the class and notice the Infolog confirming that the journal was successfully posted:
3. Optionally, open General ledger | Journals | General journal and find the processed journal to make sure it was posted.
How it works...
We create a new class called JournalPost, which will post the journal. Here, we use the system LedgerJournalCheckPost
class, which does all the work. This class ensures that all necessary
validations are performed. It also locks the journal so no other user
can process it.
All code is located in main() of JournalPost. First, we find the journal 000152_010 created in the previous recipe.
Next, we create a new jourPost object by calling newLedgerJournalTable() on LedgerJournalCheckPost. This method accepts a journal header record to be processed and a boolean argument defining that the journal should be validated and posted or validated only. In this recipe, we set NoYes::Yes, which means that the journal will be validated and posted.
Finally, we post the journal by calling run().