Thursday, December 15, 2022

D365 Create Purchase Order from Sale Order ( Direct Delivery) X++

 In my requirement, when sale order type is changed from journal to Sales order. Then purchase order must be created. Below is the code to achieve that.

[ExtensionOf(formControlStr(SalesTable, SalesTable_SalesType))]

public final class CPlDdevSalesTableForm_SalesTable_SalesType_Extension

{

  public boolean modified()

  {

    boolean ret;

    PurchCreateFromSalesOrder purchCreateFromSalesOrder = PurchCreateFromSalesOrder::construct();

    FormControl buttonControl = any2Object(this) as FormControl;

    FormRun formRun = buttonControl.formRun();

    TmpPurchLinePrice tmpPurchLinePrice;


    SalesLine salesLine, salesLineIterate;

    SalesTable salesTable;


    ret = next modified();


    salesTable = formRun.dataSource(formDataSourceStr(SalesTable, SalesTable)).cursor();


    salesLine = formRun.dataSource(formDataSourceStr(SalesTable, SalesLine)).cursor();


    if (SalesType::Sales == salesTable.SalesType)

    {

      while select salesLineIterate

        where salesLineIterate.SalesId == salesLine.SalesId

      {

        tmpPurchLinePrice.clear();


        InventTable inventTable             = salesLineIterate.inventTable();

        tmpPurchLinePrice.SalesId           = salesLineIterate.SalesId;

        tmpPurchLinePrice.LineNum           = salesLineIterate.LineNum;

        tmpPurchLinePrice.SalesLineRefRecId = salesLineIterate.RecId;

        tmpPurchLinePrice.AccountNum        = inventTable.PrimaryVendorId;

        tmpPurchLinePrice.Included          = NoYes::Yes;

        tmpPurchLinePrice.ItemId            = salesLineIterate.ItemId;

        tmpPurchLinePrice.InventDimId       = salesLineIterate.InventDimId;

        tmpPurchLinePrice.initFromInventTable(inventTable);

        tmpPurchLinePrice.PurchUnit = salesLineIterate.SalesUnit;


        if (salesTable.SalesType == SalesType::ReturnItem)

        {

          tmpPurchLinePrice.PurchQty  = salesLineIterate.ExpectedRetQty;

          tmpPurchLinePrice.PdsCWQty  = salesLineIterate.PdsCWExpectedRetQty;

        }

        else

        {

          tmpPurchLinePrice.PurchQty  = salesLineIterate.RemainSalesPhysical;

          tmpPurchLinePrice.PdsCWQty  = salesLineIterate.PdsCWRemainInventPhysical;

        }


        tmpPurchLinePrice.PurchUnit = salesLineIterate.SalesUnit;

        tmpPurchLinePrice.PurchQty = EcoResProductUnitConverter::convertGivenUnitSymbolsForReleasedProduct(tmpPurchLinePrice.ItemId,

                                                                                                                        tmpPurchLinePrice.InventDimId,

                                                                                                                        tmpPurchLinePrice.PurchQty,

                                                                                                                        salesLine.SalesUnit,

                                                                                                                        tmpPurchLinePrice.PurchUnit,

                                                                                                                        NoYes::Yes);


        tmpPurchLinePrice.PriceSearched = NoYes::No;


        if (tmpPurchLinePrice.validateWrite())

        {

          tmpPurchLinePrice.insert();

        }

      }


      purchCreateFromSalesOrder = PurchCreateFromSalesOrder::construct();

      purchCreateFromSalesOrder.parmCallerRecord(salesTable);

      purchCreateFromSalesOrder.parmTmpPurchLinePrice(tmpPurchLinePrice);

      purchCreateFromSalesOrder.parmSalesTable(salesTable);

      purchCreateFromSalesOrder.parmSalesLine(salesLine);

      purchCreateFromSalesOrder.tradeLineDlvType(tradeLineDlvType::DropShip);

      

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

      purchAutoCreate.create();

    }


    return ret;

  }

}

No comments:

Post a Comment

Build Explained

Useful Blogs. https://axtechsolutions.blogspot.com/2018/08/performing-builds-in-d365.html https://community.dynamics.com/blogs/post/?postid=...