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

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.

I have the same question (0)
  • RamaKrishna P A V Profile Picture
    16 on at
    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

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
Mallesh Deshapaga Profile Picture

Mallesh Deshapaga 304

#2
Laurens vd Tang Profile Picture

Laurens vd Tang 189 Super User 2026 Season 1

#3
André Arnaud de Calavon Profile Picture

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

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans