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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Post Product Receipt fails for split Batch/Serial lines (X++) - D365fo

(1) ShareShare
ReportReport
Posted on by 12

Hi All,

I am encountering an issue posting a Product Receipt via code after successfully posting an Item Arrival Journal.

Scenario:

  • Item Arrival: A single PurchLine was split into multiple journal lines to accommodate different Batches and Serials.

  • Status: All lines are currently "Registered" in InventTrans with their respective InventDimId.

  • The Issue: PurchFormLetter fails to post. It seems the standard logic cannot handle multiple InventDimId registrations for a single PurchLine during the automated aggregation.

  • Example: PO Line 1 (Qty: 100) is split into 4 Arrival Lines:

    • Line 1.1: Qty 30, Batch A, Serial 001,002,003

    • Line 1.2: Qty 40, Batch B, Serial 004,005,006,007

    • Line 1.3: Qty 30, Batch C, Serial 008,009,010

I would really appreciate any code snippets or guidance on this, as I've been stuck on this issue for a few days now.

I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    303,730 Super User 2026 Season 1 on at
    Hi,
     
    I do assume you get an error message when posting? Can you share the exact error message?
  • Suggested answer
    CU18110744-0 Profile Picture
    12 on at

    Hi,

    Thanks for the reply. I have tried two approaches to manually create PurchParmLine for Registered items, but I am still stuck.

    Code Set 1: I looped through WMSJournalTrans. The code runs, but no Product Receipt is created and no error message appears.

    Code Set 2: I joined PurchLine with InventTrans (Status: Registered). This time it triggers an error.

    The Error Message:

    "The state of the source document or source document line could not be updated."

    Code Set 3: no Product Receipt is created and no error message appears.

    Code Set 1 (No Error, No Receipt):

    WMSJournalTable                 wmsJournalTable = WMSJournalTable::find(JournalId);
    WMSJournalTrans                 wmsJournalTrans;
    PurchTable                      purchTable;
    PurchFormLetter_PackingSlip     purchFormLetter;
    PurchFormletterParmData         purchFormLetterParmData;
    PurchParmUpdate                 purchParmUpdate;
    PurchParmTable                  purchParmTable;
    PurchParmLine                   purchParmLine;
    PurchLine                       purchLine;
    DocumentStatus                  v_DocumentStatus = DocumentStatus::PackingSlip;
    try
    {
        ttsbegin;
        purchTable = PurchTable::find(wmsJournalTable.inventTransRefId);
        if (!purchTable.RecId)
            throw error("PurchTable not found from journal reference.");
        purchFormLetterParmData = PurchFormletterParmData::newData(v_DocumentStatus, VersioningUpdateType::Initial);
        purchFormLetterParmData.parmOnlyCreateParmUpdate(true);
        purchFormLetterParmData.createData(false);
        purchParmUpdate = purchFormLetterParmData.parmParmUpdate();
        purchParmTable.clear();
        purchParmTable.initValue();
        purchParmTable.TransDate                = today();
        purchParmTable.Num                      = wmsJournalTable.packingSlip;
        purchParmTable.Ordering                 = v_DocumentStatus;
        purchParmTable.PurchId                  = purchTable.PurchId;
        purchParmTable.ParmId                   = purchParmUpdate.ParmId;
        purchParmTable.insert();
        while select wmsJournalTrans
        where wmsJournalTrans.journalId == wmsJournalTable.journalId
        && wmsJournalTrans.inventTransRefId   == purchTable.PurchId
        {
            purchLine = PurchLine::findInventTransId(wmsJournalTrans.InventTransId);
            if (purchLine.RecId)
            {
                purchParmLine.clear();
                purchParmLine.initFromPurchLine(purchLine);
                purchParmLine.ParmId       = purchParmTable.ParmId;
                purchParmLine.TableRefId   = purchParmTable.TableRefId;
                purchParmLine.InventDimId  = wmsJournalTrans.inventDimId;
                purchParmLine.ReceiveNow   = wmsJournalTrans.qty;
                purchParmLine.setQty(v_DocumentStatus, false, true);
                purchParmLine.setLineAmount();
                purchParmLine.insert();
            }
        }
        purchFormLetter = PurchFormLetter::construct(v_DocumentStatus);
        purchFormLetter.transDate(systemDateGet());
        purchFormLetter.specQty(PurchUpdate::Recorded);
        purchFormLetter.purchTable(purchTable);
        purchFormLetter.parmId(purchParmTable.ParmId);
        purchFormLetter.purchParmUpdate(purchFormLetterParmData.parmParmUpdate());
        purchFormLetter.run();
        ttscommit;
    }
    catch (Exception::Error)
    {
        ttsabort;
        throw;
    }

    Code Set 2 (Triggering "Source document state" Error):
    public static void postProductReceiptX(PurchId purchId ,Num packingSlipId)
    {
    PurchFormLetter purchFormLetter;
    PurchFormletterParmData purchFormLetterParmData;
    PurchParmUpdate purchParmUpdate;
    PurchParmTable purchParmTable;
    PurchParmLine purchParmLine;
    PurchTable purchTable = PurchTable::find(purchId);
    PurchLine purchLine;
    InventTrans inventTrans;
    InventTransOrigin inventTransOrigin;

    ttsBegin;
    purchFormLetterParmData = PurchFormletterParmData::newData(DocumentStatus::PackingSlip, VersioningUpdateType::Initial);
    purchFormLetterParmData.parmOnlyCreateParmUpdate(true);
    purchFormLetterParmData.createData(false);
    purchParmUpdate = purchFormLetterParmData.parmParmUpdate();

    purchParmTable.clear();
    purchParmTable.initValue();
    purchParmTable.TransDate = today();
    purchParmTable.Ordering = DocumentStatus::PackingSlip;
    purchParmTable.Num = packingSlipId;
    purchParmTable.PurchId = purchTable.PurchId;
    purchParmTable.ParmId = purchParmUpdate.ParmId;
    purchParmTable.insert();

    while select purchLine join inventTrans join inventTransOrigin
    where purchLine.PurchId == purchTable.PurchId
    && inventTransOrigin.InventTransId == purchLine.InventTransId
    && inventTrans.InventTransOrigin == inventTransOrigin.RecId
    && inventTrans.StatusReceipt == StatusReceipt::Registered
    {
    purchParmLine.clear();
    purchParmLine.initFromPurchLine(purchLine);
    purchParmLine.ReceiveNow = inventTrans.Qty; // Using qty from registered trans
    purchParmLine.InventDimId = inventTrans.inventDimId;
    purchParmLine.ParmId = purchParmTable.ParmId;
    purchParmLine.TableRefId = purchParmTable.TableRefId;
    purchParmLine.setQty(DocumentStatus::PackingSlip, false, true);
    purchParmLine.setLineAmount();
    purchParmLine.insert();
    }

    purchFormLetter = PurchFormLetter::construct(DocumentStatus::PackingSlip);
    purchFormLetter.specQty(PurchUpdate::Recorded);
    purchFormLetter.purchTable(purchTable);
    purchFormLetter.parmId(purchParmTable.ParmId);
    purchFormLetter.purchParmUpdate(purchFormLetterParmData.parmParmUpdate());
    purchFormLetter.run();
    ttsCommit;
    }

    Code Set 3
    public void purchReceiptOnePO(PurchId _purchId, Num _packingSlipId)
    {
        PurchTable purchTable;
        purchTable = PurchTable::find(_purchId);
        PurchFormLetter                  purchFormLetter;
        purchFormLetter = purchFormLetter::construct(DocumentStatus::PackingSlip);
        purchFormLetter.showQueryForm(false);
        purchFormLetter.update(purchTable, _packingSlipId, today(), PurchUpdate::Recorded, AccountOrder::None, false, false);
    }
  • CU18110744-0 Profile Picture
    12 on at
    I can successfully post the Product Receipt using the standard function (UI) from the Item Arrival journal. The issue only occurs when I try to replicate this process through X++ code.
  • Diego Mancassola Profile Picture
    768 on at
    Hello, why write full custom code to create ParmLine.
    Just to reproduce the standard posting from WMSJournalTable.
    I don't have in my hand now the code to do that but if you need try to search it later
     
     
     
     
     
  • CU18110744-0 Profile Picture
    12 on at
    @Diego Mancassola

    Appreciate the help. I've implemented the DataSource and the standard Menu Item, but I might have misunderstood something because the result doesn't match my requirements yet. Any further guidance would be appreciated.
  • CU18110744-0 Profile Picture
    12 on at
    Hi, @André Arnaud de Calavon

    Could I please get your opinion?
  • André Arnaud de Calavon Profile Picture
    303,730 Super User 2026 Season 1 on at
    Hi,
     
    Have you used the debugger to check if the purchparmline records are being inserted or have you checked if they were created after your code ran?
    If you are using the debugger, you can also find the exact moment when the error is being raised. When calling your methods, are the correct values passed to the parameters of the method?
  • CU18110744-0 Profile Picture
    12 on at
    Hi,

    I have already used the debugger to trace the execution.
    Sets 1 and 3 work perfectly without any errors. However, for Set 2, I've traced back from the error message but couldn't find any missing values or point of failure in the parameters.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 658

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 468 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 333 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans