Skip to main content

Notifications

Announcements

No record found.

Supply Chain Management forum
Unanswered

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

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);        }    }}
 
  • How to update the Ordered quantity for Purch line and Order reserve quantity for SalesLine via x++
    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.
     
  • André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 287,696 Super User on at
    How to update the Ordered quantity for Purch line and Order reserve quantity for SalesLine via x++
    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?

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,696 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,490 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans