Skip to main content

Notifications

Supply chain | Supply Chain Management, Commerce
Unanswered

Create PO from SO using X++

(0) ShareShare
ReportReport
Posted on by

Hello everyone,

How the code looks if we want to create PO from Sales Order? Codes which mean using X++. I'm looking for a sample which not only can convert As-is, but able to summarize the PO, for example : 

- The code should look for all open Sales Order with same Vendor and in certain period of time into 1 PO 

- In all Sales Orders with the same vendor, which means 1 new PO, will SUM the same item into 1 PO line.

How actually we cater this, and when the PO created then we will need to identify the source SO that it is already being process and what is the PO number related to it. Also this particular SO will not included in the next PO conversion when the program run again.

Thanks in advance.

  • Create PO from SO using X++
    Hi VoltesDev 
     
    Try this X++ code to resolve the issue Creating the PO from SO
     
    static void convertSalesOrdersToPurchaseOrders()
    {
        SalesTable salesTable;
        PurchaseTable purchaseTable;
        SalesLine salesLine;
        PurchaseLine purchaseLine;
        SalesId salesId;
        VendAccount vendAccount;
        InventTable inventTable;
        Qty qty;
        // Query open Sales Orders within a specific time period
        while select salesTable
            where salesTable.SalesStatus == SalesStatus::Backorder &&
                  salesTable.CreatedDateTime >= startDate &&
                  salesTable.CreatedDateTime <= endDate
        {
            salesId = salesTable.SalesId;
            vendAccount = salesTable.VendAccount;
            // Group Sales Orders by vendor
            select sum(QtyOrdered) from salesLine
                where salesLine.SalesId == salesTable.SalesId
                group by salesLine.ItemId
                into groupSum
            {
                inventTable = InventTable::find(salesLine.ItemId);
                qty = groupSum.sum(QtyOrdered);
                // Create or update Purchase Order line
                ttsBegin;
                select forupdate purchaseTable
                    where purchaseTable.VendAccount == vendAccount &&
                          purchaseTable.OrderStatus == PurchaseStatus::Open;
                if (!purchaseTable)
                {
                    purchaseTable.initValue();
                    purchaseTable.initFromSalesOrder(salesTable); // Initialize PO from Sales Order
                    purchaseTable.insert();
                }
                purchaseLine.clear();
                select forupdate purchaseLine
                    where purchaseLine.PurchId == purchaseTable.PurchId &&
                          purchaseLine.ItemId == salesLine.ItemId;
                if (!purchaseLine)
                {
                    purchaseLine.initValue();
                    purchaseLine.PurchId = purchaseTable.PurchId;
                    purchaseLine.ItemId = salesLine.ItemId;
                    purchaseLine.createLine(inventTable);
                }
                purchaseLine.QtyOrdered += qty;
                purchaseLine.update();
                ttsCommit;
                // Update Sales Order with PO information
                ttsBegin;
                salesTable.PurchId = purchaseTable.PurchId;
                salesTable.update();
                ttsCommit;
            }
        }
    }
     

    Regards 
    RamaKrishna P A V 
     
    Note: If this works please make it as an verified

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,969 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,842 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans