web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / ShabibAX / Posting a Product Receipt w...

Posting a Product Receipt with Item Registration through code Dynamics 365 Finance and Operations X++

Shabib raza Profile Picture Shabib raza 129

Fellows, I have gone through many post giving solution of either receiving of purchase order or registration of inventory only. I am writing this blog to give a solution to receive and register purchase order in one pack.

I have created a separate class (PurchReceiving) for this to handle receiving run in a proper way.

Declared following variables in class declaration.

 PurchFormLetterParmData          purchFormLetterParmData;
PurchParmTable                   purchParmTable;
PurchParmLine                    purchParmLine;
purchParmUpdate                purchParmUpdate;
PurchTable                       purchTable;
TransDate                        packingSlipDate;
PackingSlipId                    packingSlipId;
purchFormLetter                  purchFormLetter;
PurchTableBatchReceivingTmp purchTableBatchReceivingTmp; // My customized table to receive purchase order in bulk.

- Created a method to initialize class object with required variables

 public static PurchReceiving newFromParameters(PurchTableBatchReceivingTmp purchTableBatchReceiving,
                                                PurchTable _purchTable,
                                                PackingSlipID _packingSlipID,
                                                TransDate _packingSlipDate)
{
       PurchReceiving PurchReceiving = new PurchReceiving();
       PurchReceiving.purchTableBatchReceivingTmp = purchTableBatchReceiving;
       PurchReceiving.packingSlipId = _packingSlipID;
       PurchReceiving.packingSlipDate = _packingSlipDate;
       PurchReceiving.PurchTable = _purchTable;

       return PurchReceiving;
}

- Create a run method for whole execution.

public server boolean run()
{
    boolean ret = true;     try
    {
       this.insertParmTableData();
       this.insertParmLineData();
       this.processProductReceipt();
    }
    catch
    {
       ret = checkFailed("@PostingProductReceiptError");
    }     return ret;
}

- Create a PurchParmtable record

public void insertParmTableData()
{
    purchFormLetterParmData = PurchFormletterParmData::newData(DocumentStatus::PackingSlip,VersioningUpdateType::Initial);     purchFormLetterParmData.parmOnlyCreateParmUpdate(true);
    purchFormLetterParmData.createData(false);
    purchParmUpdate = purchFormLetterParmData.parmParmUpdate();     // Set PurchParmTable table
    purchParmTable.clear();
    purchParmTable.TransDate = packingSlipDate; // which can be changed
    purchParmTable.Ordering = DocumentStatus::PackingSlip;
    purchParmTable.ParmJobStatus = ParmJobStatus::Waiting;
    purchParmTable.Num = packingSlipId; // product receipt no
    purchParmTable.PurchId = PurchTable.PurchId;
    purchParmTable.PurchName = purchTable.PurchName;
    purchParmTable.DeliveryName = purchTable.DeliveryName;
    purchParmTable.DeliveryPostalAddress = purchTable.DeliveryPostalAddress;
    purchParmTable.OrderAccount = purchTable.OrderAccount;
    purchParmTable.CurrencyCode = purchTable.CurrencyCode;
    purchParmTable.InvoiceAccount = purchTable.InvoiceAccount;
    purchParmTable.ParmId = purchParmUpdate.ParmId;
    purchParmTable.insert();
}

- Insert PurchParmLine from source table. In my case it is purchTableBatchReceivingTmp

public void insertParmLineData()
{
    InventDim inventDim;
    PurchLine purchLine;    
while select purchTableBatchReceivingTmp
         where purchTableBatchReceivingTmp.ReceiveNow > 0
    {
         purchLine = PurchLine::FindRecid(purchTableBatchReceivingTmp.PurchLineRecID);
         purchParmLine.InitFromPurchLine(purchLine);
         inventDim = purchLine.inventDim();               purchParmLine.ReceiveNow = decRound(purchTableBatchReceivingTmp.ReceiveNow, 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 = purchTableBatchReceivingTmp.BatchId;

         inventDim = inventDim::findOrCreate(inventDim); //Creating new invent dim for registration                            this.registerInventory(purchParmLine, inventDim);
    }
}

- Create registerInventory method for inventory registration.

 public void registerInventory(PurchParmLine _purchParmline,InventDim _inventDim)
{
    InventTransWMS_Register inventTransWMS_register;
    InventTrans inventTranslocal = InventTrans::findTransId(_purchParmline.InventTransId, true);
    TmpInventTransWMS tmpInventTransWMS;
    InventDim inventDimlocal = inventTranslocal.inventDim();
    InventDim InventBatchCheck;

    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);
}

- Create ProcessProductReceipt method to create product receipt.

 public void processProductReceipt()
{
    purchFormLetter = PurchFormLetter::construct(DocumentStatus::PackingSlip);
    purchFormLetter.transDate(systemDateGet());
    purchFormLetter.proforma(false);
    purchFormLetter.specQty(PurchUpdate::ReceiveNow);
    purchFormLetter.purchTable(purchTable);
    purchFormLetter.parmParmTableNum(purchParmTable.Num);
    purchFormLetter.parmId(purchParmTable.ParmId);
    purchFormLetter.purchParmUpdate(purchFormLetterParmData.parmParmUpdate());
    purchFormLetter.run();
}

Calling of this whole process:  (BONUS :P)

 PurchReceiving PurchReceiving = PurchReceiving::newFromParameters(PurchTableBatchReceivingTmp, // tmpTable buffer
                                                                   purchTable, // purchase order table
                                                                   PackingSlipNum.valueStr(), // product receipt number text box
                                                                   PackingSlipDate.dateValue() // product receipt number date);
PurchReceiving.run();

That's it,
Hope it helps.

Comments

*This post is locked for comments