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 :

Create a Purchase Order from a Sales Order Using PurchCreateFromSalesOrder Framework in X++ | D365 F&O

dasdasd Profile Picture dasdasd 841

In Dynamics 365 Finance & Operations, you may need to automatically create a Purchase Order from a Sales Order for scenarios such as intercompany processing, direct delivery, or automated procurement.

Instead of manually creating PurchTable and PurchLine, we can use the standard PurchCreateFromSalesOrder and PurchAutoCreate framework.

public static PurchId processDirectDeliveryPO (SalesTable _salesTable,  VendAccount _vendAccount)
{

    TmpPurchLinePrice         tmpPurchLinePrice;
    PurchCreateFromSalesOrder purchCreateFromSalesOrder;
    PurchAutoCreate           purchAutoCreate;
    SalesLine                 salesLine;
    PurchId                   purchId;

    delete_from tmpPurchLinePrice;

    while select salesLine
        where salesLine.SalesId == _salesTable.SalesId
    {
        if (!salesLine.ItemId)
        {
            continue;
        }

        if (salesLine.DSS_CreateFromSalesOrderConfirm != NoYes::No)
        {
            continue;
        }

        if (salesLine.referencedPurchLine().RecId)
        {
            continue;
        }

        tmpPurchLinePrice.clear();

        tmpPurchLinePrice.SalesId           = salesLine.SalesId;
        tmpPurchLinePrice.SalesLineRefRecId = salesLine.RecId;
        tmpPurchLinePrice.AccountNum        = _vendAccount;
        tmpPurchLinePrice.ItemId            = salesLine.ItemId;
        tmpPurchLinePrice.InventDimId       = salesLine.InventDimId;
        tmpPurchLinePrice.PurchQty          = salesLine.SalesQty;
        tmpPurchLinePrice.PurchUnit         = salesLine.SalesUnit;
        tmpPurchLinePrice.Included          = NoYes::Yes;

        tmpPurchLinePrice.insert();
    }

    if (!tmpPurchLinePrice.RecId)
    {
        throw error(strFmt(
            "No eligible Sales Lines found for vendor %1.",
            _vendAccount));
    }

    ttsBegin;

    purchCreateFromSalesOrder =
        PurchCreateFromSalesOrder::construct();

    purchCreateFromSalesOrder.parmCallerRecord(_salesTable);
    purchCreateFromSalesOrder.parmSalesTable(_salesTable);

    purchCreateFromSalesOrder.tradeLineDlvType(
        TradeLineDlvType::DropShip);

    purchCreateFromSalesOrder.parmTransferAddress(true);
    purchCreateFromSalesOrder.mcrDropShipment(true);

    purchAutoCreate =
        PurchAutoCreate::construct(
            tmpPurchLinePrice,
            purchCreateFromSalesOrder);

    purchAutoCreate.create();

    purchId = purchAutoCreate.purchId();

    ttsCommit;

    return purchId;
}

How It Works

The code loops through the Sales Order lines and adds only eligible lines to TmpPurchLinePrice.

It skips:

  • Lines without an item.

  • Lines that are already processed.

  • Lines already linked to a Purchase Line.

The PurchCreateFromSalesOrder framework is then configured for a DropShip/Direct Delivery scenario.

Finally, PurchAutoCreate uses the temporary records and standard D365 F&O logic to create the Purchase Order and its lines.

Where Can You Use This?

This approach can be useful for:

  • Intercompany Purchase Order creation

  • Direct Delivery / Drop Shipment

Conclusion

Using PurchCreateFromSalesOrder and PurchAutoCreate helps leverage standard D365 F&O functionality instead of manually creating PurchTable and PurchLine records. This makes the solution cleaner and easier to maintain.


This was originally posted here.

Comments

*This post is locked for comments