Friday 24 February 2023

Direct delivery PO creation from Sales order through X++

In Some scenarios, we might have to create a direct delivery Purchase order related to the available sales orders. The below code will help us in doing it.

public void creaeDirectDeliveryPO()

{    

    TmpPurchLinePrice                  tmpPurchLinePrice;

    PurchCreateFromSalesOrder    purchCreateFromSalesOrder;

    PurchAutoCreate                       purchAutoCreate;

    VendTable                            vendTable;

    SalesLine                                   salesLinePOCreation;

    SalesTable                                 salesTablePOCreation;

    AccountNum                vendAccountNum;

    LineNum                                   lineNumber;

    boolean                                      createDirectDelivery;

       

    vendAccountNum = "XYZ"; // Use the vendor account for which the PO should be created

    //Loop through the sales order lines for which the direct delivery Po lines to be created

    while select salesLinePOCreation

        join salesTablePOCreation

        where salesTablePOCreation.SalesId == "ABC" // Sales order number

        && salesLinePOCreation.SalesId == salesTablePOCreation.SalesId

    {

        if(!salesLinePOCreation.referencedPurchLine().RecId && vendAccountNum)

        {

            tmpPurchLinePrice.SalesId                      = salesLinePOCreation.SalesId;

            tmpPurchLinePrice.LineNum                  = lineNumber + 1;

            tmpPurchLinePrice.SalesLineRefRecId   = salesLinePOCreation.RecId;

            tmpPurchLinePrice.AccountNum            = vendAccountNum;

            tmpPurchLinePrice.ItemId                       = salesLinePOCreation.ItemId;

            tmpPurchLinePrice.InventDimId             = salesLinePOCreation.InventDimId;

            tmpPurchLinePrice.PurchQty                  = salesLinePOCreation.SalesQty;

            tmpPurchLinePrice.QtyOrdered              = salesLinePOCreation.SalesQty;

            tmpPurchLinePrice.PurchUnit                = salesLinePOCreation.SalesUnit;

            tmpPurchLinePrice.LineAmount            = salesLinePOCreation.LineAmount;

            tmpPurchLinePrice.Price                        = salesLinePOCreation.PriceUnit;

            tmpPurchLinePrice.CurrencyCode         = salesLinePOCreation.CurrencyCode;


            if(tmpPurchLinePrice.validateWrite())

            {

                tmpPurchLinePrice.insert();

                createDirectDelivery = true;

                lineNumber++;

            }

        }

    }


    if (createDirectDelivery)

    {

        ttsbegin;

        purchCreateFromSalesOrder   = PurchCreateFromSalesOrder::construct();

    

        purchCreateFromSalesOrder.parmCallerRecord(salesTablePOCreation);

        purchCreateFromSalesOrder.parmSalesTable(salesTablePOCreation);

        purchCreateFromSalesOrder.tradeLineDlvType(TradeLineDlvType::DropShip);

        purchCreateFromSalesOrder.parmTransferAddress(true);

        purchCreateFromSalesOrder.mcrDropShipment(true);


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

        purchAutoCreate.create();

        ttscommit;


        Info(strfmt("Direct delivery PO created is  %1", purchAutoCreate.purchId()));

    }

}