Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

AX 2009. Cannot catch error after posting a LedgerJournal from code

Posted on by 2,983

This is my code:

boolean PostJournal(ledgerJournalTable ledgerJournalTable)
{
    LedgerJournalEngine         ledgerJournalEngine;
    LedgerJournalCheckPost      ledgerJournalCheckPost;
    LedgerJournalTrans          LedgerJournalTrans;
    ;

    ledgerJournalCheckPost = LedgerJournalCheckPost::construct(ledgerJournalTable.JournalType);
    // Set the JournalId, tableId of the journalTable
    // and specify to post the journal (not only check it).

    ledgerJournalCheckPost = LedgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable,NoYes::Yes,NoYes::No);
    ledgerJournalEngine = LedgerJournalEngine::construct(ledgerJournalTable.JournalType);
    ledgerJournalEngine.newJournalActive(ledgerJournalTable,true);
    ledgerJournalCheckPost.parmLedgerJournalEngine(ledgerJournalEngine);
    try
    {
        ledgerJournalCheckPost.run();

/* This actually works, but I won't be able to retrieve the error message

        ledgerJournalTable.reread();
if (ledgerJournalTable.Posted)
return true;
else
return false;
*/
        if (!ledgerJournalCheckPost.numOfErrorsInList() && ledgerJournalCheckPost.numOfVouchersBooked())
            return true;
        else
            return false;

        while select ledgerJournalTrans where ledgerJournalTrans.JournalNum == ledgerJournalTable.JournalNum
         {
            if(LedgerJournalEngine.errorExists(ledgerJournalTrans.Voucher))
            {
               return false;
            }
        }

        if (LedgerJournalEngine.errorInJournal())
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    catch
    {
        return false;
    }
    return true;
}

For some reason, even when the journal throws error when posting, my function never returns false. Catch does not trigger, and errorExists, errorInJournal and numOfErrorsInList are always false. Any thing I need to check in my code??

There is a catch block inside  ledgerJournalCheckPost.run(); that is correctly triggered, but how can I make my functions catch block to trigger as well?

*This post is locked for comments

  • Suggested answer
    Andrew Xu Profile Picture
    Andrew Xu 3,877 on at
    RE: AX 2009. Cannot catch error after posting a LedgerJournal from code

    As you said the exception is already caught by the catch block inside of ledgerJournalCheckPost.run() which means the exception is already handled and your catch cannot see that exception. If you want to catch that exception in your catch block, you can overwrite the catch code in ledgerJournalCheckPost.run() to throw a new exception out which would be caught by your code.

    LedgerJournalCheckPost.run() is heavily used in posting ledger journals, this kind of change will impact your system quite a bit, you'd better test it thoroughly.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans