Create and post inventory ownership change journal using X++ code

In this article we will see how we can create inventory ownership change journal using X++ code in Microsoft Dynamics Finance & Operations.

We will create a new class named as InventJournalHelper and in this class we will add some methods and using those method we will create and post inventory ownership change journal.

Create a new method named as getInventJournalName in this class to get the invent journal name.

private InventJournalName getInventJournalName(RefRecId _inventJournalNameRecId = 0)
    {
        InventJournalName inventJournalName;

        select firstonly inventJournalName
            where inventJournalName.RecId == _inventJournalNameRecId;

        return inventJournalName;
    }

Create a new method named as getNextLineNumber in this class to get the next line number for inventory ownership change journal.

private LineNumber getNextLineNumber(InventJournalId _inventJournalId)
    {
        InventJournalTrans   inventJournalLine;
        LineNumber           lineNumber = 0;

        select maxof(LineNum) from inventJournalLine 
           where inventJournalLine.JournalId == _inventJournalId;

        lineNumber = inventJournalLine.LineNum;

        return lineNumber+1;
    }

Creation of Inventory ownership change journal header

Create a new method named as createInventJournalHeader in this class to create the InventJournalTable record.

public InventJournalTable createInventJournalHeader(RefRecId _inventJournalNameRecId = 0)
    {
        InventJournalTable  inventJournalHeader;
        InventJournalName   inventJournalName;

        inventJournalName = this.getInventJournalName(_inventJournalNameRecId);

        try
        {
            ttsbegin;
            //Header Creation
            inventJournalHeader.JournalNameId = inventJournalName.JournalNameId;
            inventJournalHeader.initFromInventJournalName(inventJournalName);

            inventJournalHeader.JournalId   = NumberSeq::newGetNum(InventParameters::numRefInventJournalId()).num();
            inventJournalHeader.Description = inventJournalName.Description;
            inventJournalHeader.JournalType = InventJournalType::OwnershipChange;

            if (inventJournalHeader.validateWrite())
            {
                inventJournalHeader.insert();
            }
            ttscommit;
        }
        catch
        {
            System.Exception netExcepn = CLRInterop::getLastException();
            Info(strFmt('%1', netExcepn.Message));
        }

        return inventJournalHeader;
    }

Creation of Inventory ownership change journal line

Create a new method named as initFromHeader in this class to create the InventJournalTrans record by passing the InventJournalTable record, Item Id, Quantity, Warehouse, Site, Location, From Owner, To Owner, Financial Dimension, Batch number as parameters.

public InventJournalTrans initFromHeader(
        InventJournalTable  _inventJournalHeader,
        ItemId _itemId,
        InventQtyJournal _inventQtyJournal,
        InventLocationId _inventLocationId,
        InventSiteId _inventSiteId,
        WMSLocationId _wMSLocationId,
        InventOwnerId _inventOwnerIdIssue,
        InventOwnerId _inventOwnerIdReceipt,
        InventSiteLinkedDimensionValueSet _dimensionValueSet,
        InventBatchId _inventBatchId = '')
    {
        InventJournalTrans  inventJournalLine;
        InventDim         inventDimIssue;
        InventDim         inventDimReceipt;
        InventJournalTable  inventJournalHeader = _inventJournalHeader;
        try
        {
            //Line Creation
            inventJournalLine.initValue();
            inventJournalLine.initFromInventJournalTable(_inventJournalHeader);

            inventJournalLine.LineNum           = this.getNextLineNumber(_inventJournalHeader.JournalId);
            inventJournalLine.TransDate         = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());
            inventJournalLine.ItemId            = _itemId;
            inventJournalLine.modifiedField(fieldNum(InventJournalTrans, ItemId));

            inventJournalLine.Qty               = _inventQtyJournal;
            inventJournalLine.modifiedField(fieldNum(InventJournalTrans, Qty));

            inventJournalLine.DefaultDimension     = _dimensionValueSet;

            inventDimIssue.InventSiteId           = _inventSiteId;
            inventDimIssue.InventLocationId       = _inventLocationId;
            inventDimIssue.InventOwnerId_RU       = _inventOwnerIdIssue;
            inventDimIssue.wMSLocationId          = _wMSLocationId;
            inventDimIssue.inventBatchId          = _inventBatchId;
            inventDimIssue = InventDim::findOrCreate(inventDimIssue);

            inventDimReceipt.data(inventDimIssue);
            inventDimReceipt.InventOwnerId_RU   = _inventOwnerIdReceipt;
            inventDimReceipt = InventDim::findOrCreate(inventDimReceipt);

            inventJournalLine.InventDimId       = inventDimIssue.InventDimId;
            inventJournalLine.ToInventDimId     = inventDimReceipt.InventDimId;

            if (inventJournalLine.validateWrite())
            {
                inventJournalLine.insert();
            }

            if (inventJournalLine.RecId != 0)
            {
                InventJournalTable::initTotal(inventJournalHeader);
                inventJournalHeader.update();
            }
        }
        catch
        {
            System.Exception netExcepn = CLRInterop::getLastException();
            Info(strFmt('%1', netExcepn.Message));
        }

        return inventJournalLine;
    }

Posting of Inventory ownership change journal

For posting a inventory ownership change journal we will create another method named as postInventJournal, this takes InventJournalTable record as parameter.

public void postInventJournal(InventJournalTable  _inventJournalHeader)
    {
        try
        {
            JournalCheckPost journalCheckPost;
            PurchParameters purchParameters;

            purchParameters = PurchParameters::find();

            //Post record
            journalCheckPost = InventJournalCheckPost::newPostJournal(_inventJournalHeader);
            
            journalCheckPost.runOperation();
        }
        catch
        {
            System.Exception netExcepn = CLRInterop::getLastException();
            Info(strFmt('%1', netExcepn.Message));
        }
    }

For looking over the functional process of Creation of inventory ownership change journal see the article below.

Thank you for reading this article hope you find it useful.

Omar Iqbal

Technical Consultant

References:

  1. https://www.instructorbrandon.com/optimizing-inventory-consignment-process-microsoft-dynamics-365-technical-part/
  2. https://community.dynamics.com/365/supply-chain-management/f/dynamics-365-supply-chain-management-forum/425369/post-product-receipt-of-consignment-replenishment-order-using-x
omar_iqbal

Recent Posts

Create and Update Rows Using a Plugin in Microsoft Dynamics CE (CRM)

Microsoft Dynamics CRM is a powerful Customer Relationship Management (CRM) tool that helps businesses manage…

2 years ago

Assign record owner to specific Team using Power Automate flow in Microsoft Dynamics CE (CRM)

Today we will be going over the steps how to assign owner to specific Team…

2 years ago

Set lookup in Microsoft Dynamics CE (CRM) using JavaScript

Today we will be going over the steps how to set the value of a…

2 years ago

Create and post free text invoice using X++ code

In this article we will see how we can create and post free text invoice…

3 years ago

Sales order confirmation using X++ code

In this article we will be focusing on confirmation of a sales order using X++…

3 years ago

Purchase order confirmation using X++ code

In this article we will be focusing on confirmation of a purchase order using X++…

3 years ago