2008-02-24

Posting InventJournal from X++

Once I was faced the problem to post InventJournal from code.
At first glance it's easy. Let's open AOT. Then we find corresponding Menu Item in \Menu Items\Action\InventJournalPost. Open class InventJournalCheckPost... And looking at main() method of this class we are coming to conclusion that this class is tightly tied to the journalForm. What's a pity!

So let's try to write own code.

Look at main() method:

static void main(Args args)
{
        InventJournalCheckPost journalCheckPost;
        journalForm journalForm;

        ;

        journalForm = journalForm::fromArgs(args);
        journalCheckPost = InventJournalCheckPost::newFromForm(args,journalForm);

        journalForm.runbaseMainStart();

        if (!journalCheckPost.PROMPT())
        {                 if (! journalCheckPost.BatchInfo().parmBatchExecute())                         journalForm.runbaseMainCancel();                         return;         }         try         {                 journalCheckPost.run();                 journalForm.runbaseMainEnd(journalCheckPost,false);
        }         catch (Exception::Error)         {                 journalForm.runbaseMainEnd(journalCheckPost,true);         } }
First of all we need to get rid of journalForm.
Let's rewrite newFromForm() method. The parameters wich passing into this method are used to achieve InventJournalTable. So we may just pass InventJournalTable into our new method.
JournalCheckPost getJournalCheckPost(InventJournalTable _inventJournalTable)
{
        // \Menu Items\Action\InventJournalPost
        switch(inventJournalTable.journalType)
        {
                case InventJournalType::Movement:
                case InventJournalType::LossProfit:
                case InventJournalType::Transfer:
                case InventJournalType::BOM:
                case InventJournalType::Count:
                case InventJournalType::project:
                case InventJournalType::Asset:
                        journalCheckPost_Mov = InventJournalCheckPost_Movement::newJournalCheckPost(
                        false,true, JournalCheckPostType::Post, _inventJournalTable.tableId,                    _inventJournalTable.journalId);
                        // journalTransData = _journalForm.JournalTransData();
                        // if (journalTransData)
                        // journalCheckPost_Mov.parmVoucher(journalTransData.journalTrans().voucher);
                return journalCheckPost_Mov;

                case InventJournalType::TagCounting:
                        journalCheckPost = InventJournalCheckPost_Tag::newJournalCheckPost(
                        false, true, JournalCheckPostType::Post, inventJournalTable.tableId,                    _inventJournalTable.journalId);
                return journalCheckPost_Tag;
        }
}
 
Look at JournalFormTable class. It extends journalForm class.

Inside of runbaseMainStart() method we may find following line
journalTableData.updateBlock(JournalBlockLevel::None,JournalBlockLevel::System,false);

inside of runbaseMainCancel() method:
journalTableData.updateBlock(JournalBlockLevel::System,JournalBlockLevel::None,false);

and inside of runbaseMainEnd() method:
journalTableData.updateBlock(JournalBlockLevel::System,JournalBlockLevel::None,false);

That's enough. Now we may rewrite main() method:

// Posting start. According to \Classes\InventJournalCheckPost
// journalForm.runbaseMainStart();
        journalTableData::updateBlockServer(
        inventJournalTable, JournalBlockLevel::None, JournalBlockLevel::System, false);

        journalCheckPost = getJournalCheckPost(inventJournalTable);
        if (!journalCheckPost.PROMPT())
        {
                if (! journalCheckPost.BatchInfo().parmBatchExecute())
                {
                        // journalForm.runbaseMainCancel();
                        journalTableData::updateBlockServer(
                        inventJournalTable, JournalBlockLevel::System, JournalBlockLevel::None, false);
                }
                return;
        }

        try
        {
                journalCheckPost.run();
                // journalForm.runbaseMainEnd(journalCheckPost,false);
                journalTableData::updateBlockServer(
                        inventJournalTable, JournalBlockLevel::System, JournalBlockLevel::None, false);
        }
        catch (Exception::Error)
        {
                // journalForm.runbaseMainEnd(journalCheckPost,true);
                journalTableData::updateBlockServer(
                inventJournalTable, JournalBlockLevel::System, JournalBlockLevel::None, true);
        }
// Posting end
Finally, we are ready to write our remarkable job:
static void JobInventJournalPost(Args _args)
{
        InventJournalCheckPost_Movement journalCheckPost_Mov;
        InventJournalCheckPost_Tag journalCheckPost_Tag;

        InventJournalCheckPost journalCheckPost;

        InventJournalTable inventJournalTable;
        InventJournalId inventJournalId = 'Inv002372';

        JournalCheckPost getJournalCheckPost(InventJournalTable _inventJournalTable)
        {
                // \Menu Items\Action\InventJournalPost
                switch(_inventJournalTable.journalType)
                {
                        case InventJournalType::Movement:
                        case InventJournalType::LossProfit:
                        case InventJournalType::Transfer:
                        case InventJournalType::BOM:
                        case InventJournalType::Count:
                        case InventJournalType::project:
                        case InventJournalType::Asset:
                                journalCheckPost_Mov = InventJournalCheckPost_Movement::newJournalCheckPost(
                                false,true, JournalCheckPostType::Post, _inventJournalTable.tableId,                            _inventJournalTable.journalId);
                                // journalTransData = _journalForm.JournalTransData();
                                // if (journalTransData)
                                // journalCheckPost_Mov.parmVoucher(journalTransData.journalTrans().voucher);
                                return journalCheckPost_Mov;

                        case InventJournalType::TagCounting:
                                journalCheckPost = InventJournalCheckPost_Tag::newJournalCheckPost(
                                false, true, JournalCheckPostType::Post, _inventJournalTable.tableId,                           _inventJournalTable.journalId);

                                return journalCheckPost_Tag;
                }
        }
        ;

        inventJournalTable = InventJournalTable::find(inventJournalId);
        if(inventJournalTable)
        {
                // Posting start. According to \Classes\InventJournalCheckPost
                // journalForm.runbaseMainStart();
                journalTableData::updateBlockServer(                 inventJournalTable, JournalBlockLevel::None, JournalBlockLevel::System, false);                 journalCheckPost = getJournalCheckPost(inventJournalTable);                 if (!journalCheckPost.PROMPT())                 {                         if (! journalCheckPost.BatchInfo().parmBatchExecute())                         {
                                // journalForm.runbaseMainCancel();                                 journalTableData::updateBlockServer(                                 inventJournalTable, JournalBlockLevel::System, JournalBlockLevel::None, false);                         }
                        return;
                }                 try                 {                         journalCheckPost.run();                         // journalForm.runbaseMainEnd(journalCheckPost,false);                         journalTableData::updateBlockServer(                         inventJournalTable, JournalBlockLevel::System, JournalBlockLevel::None, false);                 }                 catch (Exception::Error)                 {                         // journalForm.runbaseMainEnd(journalCheckPost,true);                         journalTableData::updateBlockServer(                         inventJournalTable, JournalBlockLevel::System, JournalBlockLevel::None, true);                 }                 // Posting end  }
Copyright © 2008 Ruslan Goncharov

6 comments:

gl00mie said...

Jesus!.. dig deeper and write a simplier code:
InventJournalCheckPost::newPostJournal(inventJournalTable).run();

Manoj said...

Hi Ruslan,
Thanks for your blogs . It is very useful for my projects.

Now im using Ax2009, Here Posting a BOM journal is bit different , i.e first it needs to finished (there should be an entry in BOMparmreportfinish table), then only it will come to inventjournaltrans table, after that only we can post . will your code help in posting a BOM journal in Ax2009

Anonymous said...

Thanks! I'm using gl00mie's example, but your blog entry is very helpfull.

Anonymous said...

Great post! By the way, do you know what is the main difference between axapta and dynamics ax? Is it just about the name?
Good luck writing your blog, you're doing great!

Lamfo said...

You are making a mistake.. just dig deeper and write a simpler code as possible otherwise it will get confusing.

Neha said...

Great post! By the way, do you know what is the main difference between axapta and dynamics CRM? Is it just about the name?
Good luck writing your blog, you're doing great!