Showing posts with label register purchase orders in D365FO. Show all posts
Showing posts with label register purchase orders in D365FO. Show all posts

Tuesday, April 1, 2025

How to register purchase orders in D365FO using X++ code

Registering a Purchase Order in X++

Code Explanation


internal final class TESTJobToCheckRegistraion
{
    PurchFormLetterParmData          purchFormLetterParmData;
    PurchParmTable                   purchParmTable;
    PurchParmLine                    purchParmLine;
    PurchParmUpdate                  purchParmUpdate;
    PurchTable                       purchTable;
    PurchFormLetter                  purchFormLetter;
    TransDate                        packingSlipDate;
    PackingSlipId                    packingSlipId;

    public static void main(Args _args)
    {
        PurchTable    purchTable;
        PurchLine     purchLine;
        TransDate     packingSlipDate;
        PackingSlipId packingSlipId;
        container     lineNums;
        container     purchLineRecIds;
        container     inventBatchIds;
        container     qtys;
        int           i;

        purchTable      = PurchTable::find('002357-POR-UK01');
        packingSlipDate = DateTimeUtil::date(DateTimeUtil::utcNow());
        packingSlipId   = 'PK0001';

        lineNums       = [100]; // purch line numbers
        inventBatchIds = ['Batch001']; //batch numbers to register
        qtys           = [100]; // quantity to register

        for (i = 1; i <= conLen(lineNums); i++)
        {
            purchLine = PurchLine::find(purchTable.PurchId, conPeek(lineNums, i));
            purchLineRecIds = conIns(purchLineRecIds, conLen(purchLineRecIds) + 1, purchLine.RecId);
        }

        TESTJobToCheckRegistraion TESTJobToCheckRegistraion = new TESTJobToCheckRegistraion();
        TESTJobToCheckRegistraion.insertParmLineData(purchLineRecIds, inventBatchIds, qtys);
    }

    private void registerInventory(PurchParmLine _purchParmline, InventDim _inventDim)
    {
        InventTransWMS_Register inventTransWMS_register;
        TmpInventTransWMS       tmpInventTransWMS;
        InventDim               inventBatchCheck;
        InventTrans             inventTranslocal;
        InventDim               inventDimlocal;

        inventTranslocal = InventTrans::findTransId(_purchParmline.InventTransId, true);
        inventDimlocal   = inventTranslocal.inventDim();
        inventDimlocal.inventBatchId    = _inventDim.inventBatchId;
        inventDimlocal.InventLocationId = _inventDim.InventLocationId;
        inventDimlocal.InventSiteId     = _inventDim.InventSiteId;
        inventDimlocal                  = InventDim::findOrCreate(inventDimlocal);
        inventTransWMS_register         = inventTransWMS_register::newStandard(tmpInventTransWMS);

        inventTranslocal.inventDimId    = inventDimlocal.inventDimId;
        tmpInventTransWMS.clear();
        tmpInventTransWMS.initFromInventTrans(inventTranslocal);
        tmpInventTransWMS.ItemId    = inventTranslocal.ItemId;
        tmpInventTransWMS.InventQty = _purchParmline.ReceiveNow;
        tmpInventTransWMS.insert();

        inventTransWMS_register.writeTmpInventTransWMS(tmpInventTransWMS, inventTranslocal, inventDimlocal);
        inventTransWMS_register.updateInvent(inventTranslocal);
    }

    private void insertParmLineData(container _purchLineRecIds, container _inventBatchIds, container _qtys)
    {
        PurchLine purchLine;
        InventDim inventDim;
        int       i;

        for (i = 1; i <= conLen(_inventBatchIds); i++)
        {
            purchLine.clear();
            purchLine = PurchLine::findRecId(conPeek(_purchLineRecIds, i));
            purchParmLine.InitFromPurchLine(purchLine);
            inventDim = purchLine.inventDim();
            purchParmLine.ReceiveNow  = decRound(conPeek(_qtys, i), 2);
            purchParmLine.InventDimId = inventDim.inventDimId;
            purchParmLine.ParmId      = purchParmTable.ParmId;
            purchParmLine.TableRefId  = purchParmTable.TableRefId;
            purchParmLine.setQty(DocumentStatus::PackingSlip, false, true);
            purchParmLine.setLineAmount();
            purchParmLine.insert();
            inventDim.inventBatchId   = conPeek(_inventBatchIds, i);
            inventDim = inventDim::findOrCreate(inventDim);
            this.registerInventory(purchParmLine, inventDim);
        }
    }
}

   


Update Values During Data Entity Import in D365FO

Update Values During Data Entity Import in D365FO Updating Field Values During Data Entity Import in Dynamics 365 Finance ...