āœļø Explanatory Question

In Dynamics 365 Finance & Operations (D365 F&O), you need to create a custom script that processes sales order lines (SalesLine) and updates inventory (InventSum) under specific conditions.

šŸ‘ 28 Views
šŸ“˜ Detailed Answer
🟢 Easy
28
Total Views
1
Related Qs
0%
Progress
šŸ’”

Answer with Explanation

Question:
In Dynamics 365 Finance & Operations (D365 F&O), you need to create a custom script that processes sales order lines (SalesLine) and updates inventory (InventSum) under specific conditions.

Requirements:

  1. The script should iterate over a list of predefined Sales Orders.
  2. It should check for SalesLine records that meet the following criteria:
    • ReturnStatus is Awaiting.
    • SalesType is ReturnItem.
    • ExpectedRetQty is greater than 0.
  3. If the above conditions are met, retrieve the corresponding SalesTable to get Site ID and Warehouse ID.
  4. Locate InventSum records where:
    • InventDimId matches SalesLine.InventDimId.
    • wMSLocationId is Scrap.
    • InventSiteId and InventLocationId match the SalesTable values.
  5. If a matching InventSum record exists, update its OnOrder value to 0.0 and commit the transaction.

Implementation Challenge:
Write an X++ script that fulfills these requirements.
šŸ’” Bonus: How can you optimize the query to reduce database hits? šŸš€

Here's your X++ script with comments for better understanding: x++ Copy Edit


internal final class USVRODuplicateLinesDeletion  
{  
    /// <summary>  
    /// Class entry point. The system will call this method when a designated menu  
    /// is selected or when execution starts and this class is set as the startup class.  
    /// </summary>  
    /// <param name = "_args">The specified arguments.</param>  
    public static void main(Args _args)  
    {  
        SalesLine salesLine;  
        InventSum inventSum;  

        // Container holding predefined sales order IDs  
        container salesOrders =  
        [  
            "SO0012957165", "SO0012814508", "SO0011627781", "SO0011286642",  
            "SO0011269270", "SO0011080545", "SO0010922251", "SO0010888854",  
            "SO0010477843", "SO0010395732", "SO0010331859", "SO0010285584",  
            "SO0010026866", "SO0009882690", "SO0009539529", "SO0009526478",  
            "SO0007686061", "SO0007279827"  
        ];  

        // Loop through each sales order ID in the container  
        for (int i = 1; i <= conLen(salesOrders); i++)  
        {  
            // Selecting SalesLine records where:  
            // - SalesId matches the current sales order ID  
            // - ReturnStatus is "Awaiting"  
            // - SalesType is "ReturnItem"  
            // - ExpectedRetQty is greater than zero  
            while select salesLine  
                where salesLine.SalesId == conPeek(salesOrders, i)  
                && salesLine.ReturnStatus == ReturnStatusLine::Awaiting  
                && salesLine.SalesType == SalesType::ReturnItem  
                && salesLine.ExpectedRetQty > 0  
            {  
                // Fetch SalesTable record corresponding to the SalesId  
                SalesTable salesTable = SalesTable::find(salesLine.SalesId);  

                // Get Site ID and Warehouse ID from SalesTable  
                InventSiteId siteId = salesTable.InventSiteId;  
                InventLocationId warehouseId = salesTable.InventLocationId;  

                // Begin transaction block  
                ttsbegin;  

                // Fetch the corresponding InventSum record  
                select forupdate inventSum  
                    where inventSum.InventDimId == salesLine.InventDimId  
                    && inventSum.wMSLocationId == "Scrap"  
                    && inventSum.InventSiteId == siteId  
                    && inventSum.InventLocationId == warehouseId;  

                // If an InventSum record exists, update its OnOrder quantity to 0.0  
                if (inventSum.RecId)  
                {  
                    inventSum.OnOrder = 0.0;  
                    inventSum.doUpdate();  
                }  

                // Commit the transaction  
                ttscommit;  
            }  
        }  
    }  
}