AX – D365FO – Create and post a Transfer InventJournal in Dynamics AX 2012

The sample code below creates a Transfer InventJournal and posts it

public static boolean createInventTransferJournal()
{
    InventDim       fromInventDim;
    InventDim       toInventDim;

    InventJournalNameTransfer   journalIdParameter;
    InventJournalName           transferJournalName;
    InventJournalTable          inventJournalTable;
    InventJournalTable          updateInventJournalTable;
    InventJournalTrans          inventJournalTrans;
    JournalCheckPost            inventJournalCheckPost;  
    ;

    try
    {
        ttsBegin;

        journalIdParameter = InventParameters::find().TransferJournalNameId;

        if (journalIdParameter == '')
        {
            return false;
        }

        transferJournalName = InventJournalName::find(journalIdParameter);

        //Create invent journal header
        inventJournalTable.clear();
        inventJournalTable.initFromInventJournalName(transferJournalName);
        inventJournalTable.Description = 'Whatever description you want to provide'; //Change accordingly
        inventJournalTable.SystemBlocked = true;
        inventJournalTable.insert();
        
        //Create invent journal line - in this example, we create one line.
        //Normally, you'd probably make a loop to create more...
        inventJournalTrans.clear();
        inventJournalTrans.initFromInventJournalTable(inventJournalTable);
        inventJournalTrans.ItemId = '20000039'; //Change accordingly
        inventJournalTrans.LineNum = 1; //Change accordingly
        
        //Set From InventDimId
        fromInventDim.InventLocationId = '001'; //Change accordingly
        fromInventDim.InventSiteId = '012'; //Change accordingly
        fromInventDim.InventSizeId = 'XL'; //Change accordingly
        fromInventDim.InventColorId = 'BLUE'; //Change accordingly
        //TODO: Set other important inventDim properties here if applicable
        fromInventDim = InventDim::findOrCreate(fromInventDim);
                
        //Set To InventDimId
        toInventDim.InventLocationId = '001'; //Change accordingly
        toInventDim.InventSiteId = '012'; //Change accordingly
        toInventDim.InventSizeId = 'XXXL'; //Change accordingly
        toInventDim.InventColorId = 'RED'; //Change accordingly
        //TODO: Set other important inventDim properties here if applicable
        toInventDim = InventDim::findOrCreate(toInventDim);
        
        inventJournalTrans.InventDimId = frominventDim.inventDimId;
        inventJournalTrans.initFromInventTable(InventTable::find('20000039'));
        inventJournalTrans.Qty = 1; //Change accordingly
        inventJournalTrans.ToInventDimId = ToinventDim.inventDimId;
        inventJournalTrans.TransDate = today();
        inventJournalTrans.insert();
        
        //Post journal
        inventJournalCheckPost = InventJournalCheckPost::newJournalCheckPost(JournalCheckpostType::Post, inventJournalTable);
        inventJournalCheckPost.parmThrowCheckFailed(true);
        inventJournalCheckPost.parmShowInfoResult(false);
        inventJournalCheckPost.run();

        select firstOnly forUpdate updateInventJournalTable
            where updateInventJournalTable.RecId == inventJournalTable.RecId;

        updateInventJournalTable.SystemBlocked = false;
        updateInventJournalTable.NumOfLines = 1; //Change accordingly
        updateInventJournalTable.update();

        ttsCommit;
    }
    catch
    {
        ttsAbort;
        return false;
    }

    return true;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s