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 :
Supply chain | Supply Chain Management, Commerce
Unanswered

How to update the Ordered quantity for Purch line and Order reserve quantity for SalesLine via x++

(0) ShareShare
ReportReport
Posted on by 2
Hi All,
 
how to update the ordered qty in case of purchase order in Purchline , and order reserve qty in order reserver in case of sales order in salesline.
 
I have compared the behaviour in standard via the below screen and it does updates the both delivery remainder and the orderedinOurchUnit quantity and also the order reserve in case of sales order.
 
 
Please see my code it does updates the delivery remainder properly, but it is not updating the ordered quantity in case of purchase order and not updating the order reserve quantity in case of sales order. 
 
Please suggest what am i doing wrong here
 
public class CNLHeroPurchCreationDeliveryRemainder{    public static CNLHeroPurchCreationDeliveryRemainder construct()    {        CNLHeroPurchCreationDeliveryRemainder delRemainder = new CNLHeroPurchCreationDeliveryRemainder();        return delRemainder;    }    public static void cancelRemainPhysical(PurchLine _purchLine, PurchQty _qty)    {        CNLHeroPurchCreationDeliveryRemainder    deliverRemainder = new CNLHeroPurchCreationDeliveryRemainder();        ttsbegin;        PurchQty RemainPurchPhys = _purchLine.RemainPurchPhysical;        PurchQty qtyRemain = _purchLine.salesPurchLineInterface().calcSalesPurchQty(_qty);        _purchLine.RemainPurchPhysical  = qtyRemain;        _purchLine.RemainInventPhysical = qtyRemain;        if (_purchLine.RemainInventPhysical)        {            if (!_purchLine.RemainPurchPhysical)            {                throw error(/@SCM:InventoryQuantityCannotBe0WhenOrderQuantityIs0/);            }            if (sign(_purchLine.RemainInventPhysical) != sign(_purchLine.RemainPurchPhysical))            {                throw error(/@SCM:InventoryQuantityMustHaveSameSignAsOrderQuantity/);            }        }        if (_purchLine.validateWrite() && _purchLine.purchTable().checkUpdate())        {            deliverRemainder.updateRemainPhysical(_purchLine,qtyRemain);            // Update project commitments when deliver remainder is updated or quantity is canceled.            deliverRemainder.updateProjCommitment(_purchLine, RemainPurchPhys);                            if (_purchLine.PurchStatus  == PurchStatus::Canceled &&                _purchLine.PurchaseType == PurchaseType::ReturnItem &&                _purchLine.isInterCompanyOrder())            {                if (_purchLine.InterCompanyInventTransId)                {                    changecompany(_purchLine.purchTable().InterCompanyCompanyId)                    {                        var interCompanySalesLine = SalesLine::findInventTransId(_purchLine.InterCompanyInventTransId);                        var interCompanySalesTable = SalesTable::find(interCompanySalesLine.SalesId);                            if (interCompanySalesTable.ReturnStatus == ReturnStatusHeader::Created &&                            !interCompanySalesTable.existRegisteredReceivedInvoicedLines())                        {                            interCompanySalesLine.SalesStatus = SalesStatus::Canceled;                            interCompanySalesLine.ReturnStatus = ReturnStatusLine::Canceled;                            interCompanySalesLine.doUpdate();                        }                                                    interCompanySalesTable.SalesStatus = SalesTableType::construct(interCompanySalesTable).lowestSalesStatus();                        if (interCompanySalesTable.SalesStatus == SalesStatus::Canceled)                        {                            interCompanySalesTable.ReturnStatus = ReturnStatusHeader::Canceled;                            interCompanySalesTable.doUpdate();                        }                    }                }            }        }        if (_purchLine.purchTable().PurchStatus == PurchStatus::Canceled)        {            PurchPrepayRemove::reversePrepaymentInvoice(_purchLine.PurchId);        }        ttscommit;    }    /// <summary>    /// Retrieves the flag controlling which parts of the intercompany update process are skipped.    /// </summary>    /// <param name = /_callerPurchLine/>    /// The caller purchase order line record.    /// </param>    /// <returns>    /// The intercompany skip update flag.    /// </returns>    [SuppressBpWarning(/BPParameterNotUsed/, /Parameter _callerPurchLine is used by extensions that wrap this method./)]    public InterCompanySkipUpdate retrieveInterCompanySkipUpdateFlag(PurchLine _callerPurchLine)    {        return InterCompanySkipUpdate::No;    }    public void updateRemainPhysical(PurchLine _callerPurchLine, PurchQty _qtyRemain)    {        PurchLine       purchLineLocal = PurchLine::findRecId(_callerPurchLine.RecId);        PurchLine       purchlineUpdate;        InventQty       diffRemainPurchPhysical       = purchLineLocal.RemainPurchPhysical       - _qtyRemain;        InventQty       diffRemainInventPhysical      = purchLineLocal.RemainInventPhysical      - _qtyRemain;        PdsCWInventQty  diffPdsCWRemainInventPhysical;  // = purchLineLocal.PdsCWRemainInventPhysical - _qtyRemain;        MCRSalesOrderCancellation mcrSalesOrderCancellation;        PurchSalesOrderCancellation purchSalesOrderCancellation = new PurchSalesOrderCancellation();        mcrSalesOrderCancellation = purchSalesOrderCancellation.mcrSalesOrderCancelInit(mcrSalesOrderCancellation, purchLineLocal.InventRefTransId);        InterCompanyUpdateRemPhys::synchronize(_callerPurchLine,                                            diffRemainInventPhysical,                                            diffRemainPurchPhysical,                                            this.retrieveInterCompanySkipUpdateFlag(_callerPurchLine),                                            diffPdsCWRemainInventPhysical);        if (_callerPurchLine.ItemRefType  == InventRefType::Sales                && _callerPurchLine.InventRefTransId                && _callerPurchLine.isDropShipment())        {            ttsbegin;                select forupdate purchlineUpdate                    where purchlineUpdate.RecId == _callerPurchLine.RecId;                if (purchlineUpdate.RecId)                {                    purchlineUpdate.RemainPurchPhysical         = _qtyRemain;                    purchlineUpdate.RemainInventPhysical        = _qtyRemain;                    // purchlineUpdate.PdsCWRemainInventPhysical   = _qtyRemain;                    if (_qtyRemain == 0)                    {                        purchlineUpdate.PurchStatus                 = PurchStatus::Canceled;                    }                    purchlineUpdate.doUpdate();                }            ttscommit;        }                purchSalesOrderCancellation.mcrSalesOrderCancel(mcrSalesOrderCancellation, purchLineLocal.InventRefTransId);        // This part is related to Europe Extension        MCRSalesLineDropShipment mcrSalesLineDropShipment;        SalesLine                salesLine = SalesLine::findInventTransId(purchlineUpdate.InventRefTransId);        mcrSalesLineDropShipment = MCRSalesLineDropShipment::findSalesLine(salesLine.RecId, true);        if(mcrSalesLineDropShipment)        {            mcrSalesLineDropShipment.DropShipment = NoYes::No;            mcrSalesLineDropShipment.DropShipStatus = MCRDropShipStatus::None;            mcrSalesLineDropShipment.update();        }    }    public void updateProjCommitment(PurchLine _callerPurchLine, PurchQty _remainPurchPhysicalOrig)    {        if ((_callerPurchLine.RemainPurchPhysical && _remainPurchPhysicalOrig != _callerPurchLine.RemainPurchPhysical)                || (!_callerPurchLine.RemainPurchPhysical))        {            SourceDocumentLine sourceDocumentLine = SourceDocumentLine::find(_callerPurchLine.SourceDocumentLine);            sourceDocumentLineItem sourceDocumentLineItem = sourceDocumentLineItem::newFromSourceDocumentLine(sourceDocumentLine);            ProjectCommitmentFacade::updateProjectCommitments(sourceDocumentLineItem);        }    }}
 
I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    304,648 Super User 2026 Season 1 on at
    Hi,
     
    Can you repost the coding in a new reply to this question? The visibility of coding is not correct in a question, but it will be readable in a reply.

    Have you checked the coding used in the standard behind the deliver remainder update feature?
  • NN-12051955-0 Profile Picture
    2 on at
    public class CNLHeroPurchCreationDeliveryRemainder
    {
        public static CNLHeroPurchCreationDeliveryRemainder construct()
        {
            CNLHeroPurchCreationDeliveryRemainder delRemainder = new CNLHeroPurchCreationDeliveryRemainder();
            return delRemainder;
        }
    
        public static void cancelRemainPhysical(PurchLine _purchLine, PurchQty _qty)
        {
            CNLHeroPurchCreationDeliveryRemainder    deliverRemainder = new CNLHeroPurchCreationDeliveryRemainder();
            ttsbegin;
            PurchQty RemainPurchPhys = _purchLine.RemainPurchPhysical;
            PurchQty qtyRemain = _purchLine.salesPurchLineInterface().calcSalesPurchQty(_qty);
    
            _purchLine.RemainPurchPhysical  = qtyRemain;
            _purchLine.RemainInventPhysical = qtyRemain;
    
            if (_purchLine.RemainInventPhysical)
            {
                if (!_purchLine.RemainPurchPhysical)
                {
                    throw error("@SCM:InventoryQuantityCannotBe0WhenOrderQuantityIs0");
                }
                if (sign(_purchLine.RemainInventPhysical) != sign(_purchLine.RemainPurchPhysical))
                {
                    throw error("@SCM:InventoryQuantityMustHaveSameSignAsOrderQuantity");
                }
            }
    
            if (_purchLine.validateWrite() && _purchLine.purchTable().checkUpdate())
            {
                deliverRemainder.updateRemainPhysical(_purchLine,qtyRemain);
    
                // Update project commitments when deliver remainder is updated or quantity is canceled.
                deliverRemainder.updateProjCommitment(_purchLine, RemainPurchPhys);
                    
                if (_purchLine.PurchStatus  == PurchStatus::Canceled &&
                    _purchLine.PurchaseType == PurchaseType::ReturnItem &&
                    _purchLine.isInterCompanyOrder())
                {
                    if (_purchLine.InterCompanyInventTransId)
                    {
                        changecompany(_purchLine.purchTable().InterCompanyCompanyId)
                        {
                            var interCompanySalesLine = SalesLine::findInventTransId(_purchLine.InterCompanyInventTransId);
                            var interCompanySalesTable = SalesTable::find(interCompanySalesLine.SalesId);
        
                            if (interCompanySalesTable.ReturnStatus == ReturnStatusHeader::Created &&
                                !interCompanySalesTable.existRegisteredReceivedInvoicedLines())
                            {
                                interCompanySalesLine.SalesStatus = SalesStatus::Canceled;
                                interCompanySalesLine.ReturnStatus = ReturnStatusLine::Canceled;
                                interCompanySalesLine.doUpdate();
                            }
                                
                            interCompanySalesTable.SalesStatus = SalesTableType::construct(interCompanySalesTable).lowestSalesStatus();
                            if (interCompanySalesTable.SalesStatus == SalesStatus::Canceled)
                            {
                                interCompanySalesTable.ReturnStatus = ReturnStatusHeader::Canceled;
                                interCompanySalesTable.doUpdate();
                            }
                        }
                    }
                }
            }
    
            if (_purchLine.purchTable().PurchStatus == PurchStatus::Canceled)
            {
                PurchPrepayRemove::reversePrepaymentInvoice(_purchLine.PurchId);
            }
    
            ttscommit;
        }
    
        /// <summary>
        /// Retrieves the flag controlling which parts of the intercompany update process are skipped.
        /// </summary>
        /// <param name = "_callerPurchLine">
        /// The caller purchase order line record.
        /// </param>
        /// <returns>
        /// The intercompany skip update flag.
        /// </returns>
        [SuppressBpWarning("BPParameterNotUsed", "Parameter _callerPurchLine is used by extensions that wrap this method.")]
        public InterCompanySkipUpdate retrieveInterCompanySkipUpdateFlag(PurchLine _callerPurchLine)
        {
            return InterCompanySkipUpdate::No;
        }
    
        public void updateRemainPhysical(PurchLine _callerPurchLine, PurchQty _qtyRemain)
        {
            PurchLine       purchLineLocal = PurchLine::findRecId(_callerPurchLine.RecId);
            PurchLine       purchlineUpdate;
    
            InventQty       diffRemainPurchPhysical       = purchLineLocal.RemainPurchPhysical       - _qtyRemain;
            InventQty       diffRemainInventPhysical      = purchLineLocal.RemainInventPhysical      - _qtyRemain;
            PdsCWInventQty  diffPdsCWRemainInventPhysical;  // = purchLineLocal.PdsCWRemainInventPhysical - _qtyRemain;
    
            MCRSalesOrderCancellation mcrSalesOrderCancellation;
            PurchSalesOrderCancellation purchSalesOrderCancellation = new PurchSalesOrderCancellation();
            mcrSalesOrderCancellation = purchSalesOrderCancellation.mcrSalesOrderCancelInit(mcrSalesOrderCancellation, purchLineLocal.InventRefTransId);
    
            InterCompanyUpdateRemPhys::synchronize(_callerPurchLine,
                                                diffRemainInventPhysical,
                                                diffRemainPurchPhysical,
                                                this.retrieveInterCompanySkipUpdateFlag(_callerPurchLine),
                                                diffPdsCWRemainInventPhysical);
    
            if (_callerPurchLine.ItemRefType  == InventRefType::Sales
                    && _callerPurchLine.InventRefTransId
                    && _callerPurchLine.isDropShipment())
            {
                ttsbegin;
                    select forupdate purchlineUpdate
                        where purchlineUpdate.RecId == _callerPurchLine.RecId;
                    if (purchlineUpdate.RecId)
                    {
                        purchlineUpdate.RemainPurchPhysical         = _qtyRemain;
                        purchlineUpdate.RemainInventPhysical        = _qtyRemain;
                        // purchlineUpdate.PdsCWRemainInventPhysical   = _qtyRemain;
                        if (_qtyRemain == 0)
                        {
                            purchlineUpdate.PurchStatus                 = PurchStatus::Canceled;
                        }
                        purchlineUpdate.doUpdate();
                    }
                ttscommit;
            }
            
    
            purchSalesOrderCancellation.mcrSalesOrderCancel(mcrSalesOrderCancellation, purchLineLocal.InventRefTransId);
    
            // This part is related to Europe Extension
            MCRSalesLineDropShipment mcrSalesLineDropShipment;
            SalesLine                salesLine = SalesLine::findInventTransId(purchlineUpdate.InventRefTransId);
    
            mcrSalesLineDropShipment = MCRSalesLineDropShipment::findSalesLine(salesLine.RecId, true);
            if(mcrSalesLineDropShipment)
            {
                mcrSalesLineDropShipment.DropShipment = NoYes::No;
                mcrSalesLineDropShipment.DropShipStatus = MCRDropShipStatus::None;
                mcrSalesLineDropShipment.update();
            }
        }
    
        public void updateProjCommitment(PurchLine _callerPurchLine, PurchQty _remainPurchPhysicalOrig)
        {
            if ((_callerPurchLine.RemainPurchPhysical && _remainPurchPhysicalOrig != _callerPurchLine.RemainPurchPhysical)
                    || (!_callerPurchLine.RemainPurchPhysical))
            {
                SourceDocumentLine sourceDocumentLine = SourceDocumentLine::find(_callerPurchLine.SourceDocumentLine);
                sourceDocumentLineItem sourceDocumentLineItem = sourceDocumentLineItem::newFromSourceDocumentLine(sourceDocumentLine);
                ProjectCommitmentFacade::updateProjectCommitments(sourceDocumentLineItem);
            }
        }
    
    }
     
    HI Andre,
     
    Thank for your response.
    Please see the above code and its as per standard only with little modification in it.
     

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 > Supply chain | Supply Chain Management, Commerce

#1
André Arnaud de Calavon Profile Picture

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

#2
Laurens vd Tang Profile Picture

Laurens vd Tang 126 Super User 2026 Season 1

#3
Zain Mehmood Profile Picture

Zain Mehmood 89 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans