axaptadynamics-ax-2009microsoft-dynamics

LedgerJournalEngine and LedgerJournalCheckPost


I am creating and posting Dynamics AX Ledger Journals from C#.

I want to use the two helper classes the come with AX,

LedgerJournalEngine and LedgerJournalCheckPost, to validate the journals I create.

My questions are:

1.) How do you get a list of errors -> voucher from either of these classes or some other class?

2.) Can you simulate a post inside of a AX transaction and roll it back?

2-a.) If you roll back a posting in a transaction will AX be smart enough to reuse the voucher numbers that got rolled back?


Solution

  • I ended up with:

    public static ERSImport_Errors PostJournal(int64 _journalRecID)
    {
        LedgerJournalTable          ledgerJournaltable;
        LedgerJournalCheckPost      ledgerJournalCheckPost;
        LedgerJournalID             errorJournalID;
        LedgerJournalEngine         lje;
        ERSImport_Errors             errors;
    
        boolean                     ret = true;//True we posted this journalRecID
        LedgerJournalTrans          ledgerJournalTrans;
        ;
    
        errors = new ERSImport_Errors();
        select crosscompany ledgerjournaltable where ledgerjournaltable.RecId == _journalRecID;
    
        if(ledgerJournalTable == null)
            throw error("Could not find ledger journal table provided");
    
        changecompany(ledgerJournalTable.dataAreaId)
        {
            ledgerJournalCheckPost = LedgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable,NoYes::Yes,NoYes::No);
            lje = LedgerJournalEngine::construct(ledgerJournalTable.JournalType);
            lje.newJournalActive(ledgerJournalTable,true);
            ledgerJournalCheckPost.parmLedgerJournalEngine(lje);
            try
            {
                ledgerJournalCheckPost.run();
            }
            catch
            {
                while select ledgerJournalTrans where ledgerJournalTrans.JournalNum == ledgerJournalTable.JournalNum
                {
                    if(lje.errorExists(ledgerJournalTrans.Voucher))
                    {
                        errors.addError(lje.errorLog(ledgerJournalTrans.Voucher),ledgerJournalTrans.RecId);
                    }
                }
            }
        }
        return errors;
    }