X++ code to create purchase order for a sale order if physical inventory is not available for a particular sale order

 class BASPurchOrderForSaleOrderJob

{

    /// <summary>

    /// Runs the class with the specified arguments.

    /// </summary>

    /// <param name = "_args">The specified arguments.</param>

    public static void main(Args _args)

    {

        //SalesId which I want to create Purchse Order

        SalesId salesId = "014754";

        int countLines;

        SalesLine salesLine;

        InventDim inventDim;

        InventSum inventSum;

        //Checkung On hand inventory 

        while select SalesId,ItemId from salesLine

            where salesLine.SalesId == salesId

            join InventSiteId,InventLocationId from inventDim

            where inventDim.inventDimId == salesLine.InventDimId

            join PhysicalInvent from inventSum

            where inventSum.ItemId == salesLine.ItemId

            && inventSum.InventDimId == inventDim.inventDimId

            && inventSum.PhysicalInvent <= 0

        {

            countLines++;

        }

        //If Physical inventory is empty creates a PO

        if(countLines > 0)

        {

            //Data entity declaration

            PurchPurchaseOrderHeaderV2Entity  purchTable;

            PurchPurchaseOrderLineV2Entity    purchLine;

            //Create Purchase header

            purchTable.initValue();

            //Vendor account number

            purchTable.OrderVendorAccountNumber    = "001101";

            //Inserting the SalesId 

            purchTable.InterCompanyOriginalSalesId = salesId;

            purchTable.insert();

            //Fetching PurchId to create lines

            PurchId purchid = purchTable.PurchaseOrderNumber;


            //Create Purchase lines and looping through to get particulat itemId SiteId LocationId

            while select salesId,ItemId,SalesQty from salesLine

            where salesLine.SalesId == salesId

            join  InventSiteId,InventLocationId from inventDim

            where inventDim.inventDimId == salesLine.InventDimId

            join PhysicalInvent from inventSum

            where inventSum.ItemId == salesLine.ItemId

            && inventSum.InventDimId == inventDim.inventDimId

            && inventSum.PhysicalInvent <= 0

        

            {

                purchLine.initValue();

                purchLine.PurchaseOrderNumber         = purchid;

                purchLine.ItemNumber                  = salesLine.ItemId;

                purchLine.OrderedPurchaseQuantity     = salesLine.SalesQty;

                purchLine.ReceivingSiteId             = inventDim.InventSiteId;

                purchLine.ReceivingWarehouseId        = inventDim.InventLocationId;

                purchLine.insert();

                purchLine.clear();

            }

     

            Info(strFmt("Purchase order %1 has been created.",purchTable.PurchaseOrderNumber));


            //Update PO by using PurchFormLetter class

            PurchFormLetter            purchFormLetter;

            // Get Purchase Order

            PurchTable PurchUpdate   = PurchTable::find(purchId);


            //Posting PO Confirmation,I guess its mandatory

            //You cannot do invoice without doing PO confirm

            purchFormLetter = PurchFormLetter::construct(DocumentStatus::PurchaseOrder);

            purchFormLetter.update(PurchUpdate, strFmt("PO_%1", purchTable.PurchaseOrderNumber));

            ;

            //Posting Product receipt

            purchFormLetter = PurchFormLetter::construct(DocumentStatus::PackingSlip);

            purchFormLetter.update(PurchUpdate, strFmt("PO_%1", purchTable.PurchaseOrderNumber));

            info("Product receipt posted");

            ;

            //Posting PO Invoice

            purchFormLetter = PurchFormLetter::construct(DocumentStatus::Invoice);

            purchFormLetter.update(PurchUpdate, strFmt("Inv_%1", purchTable.PurchaseOrderNumber));

            info("Invoice posted");

        }

        else

        {

            warning(strFmt("Sales Order %1 has Physical Inventory",salesId));

        }

    }

}

Comments

Popular posts from this blog

how to post trade agreement journals automatically using x++ code

How to Create a wizard in x++ d365

x++ code to submit and approve and reject the invent movement workflow in d365 F&O