Create Item Requirement from Item Forecast using X++ in D365 Operations
Introduction:
In this blog article, we will see how we can create Item Requirement on insertion of Item Forecast using code.
Steps:
- Create Extension Class for ForecastSales Table that is Item Forecast and using CoC for post insert() method we will initialize Item Requirement. We will call a new class which is created in Step 2.
- Create a new class that will initialize values and insert record in Item Requirement form.
- In the new class create a method initParameter. This method will initialize ForecastSales object.
- Create another method ‘copytoSalesLine’. It will validate the record and call other methods to copy values to SalesLine Table.
- Create a new method ‘initializeSalesLine’. It is called from copyToSalesLine().
- Create a new method updateSalesLine(). It is called from copyToSalesLine() method.
public void insert() { next insert(); SalesType salesType = SalesType::ItemReq; CFSCreateItemReqFrmItemForecast createItemReq = new CFSCreateItemReqFrmItemForecast(); createItemReq.initParameters(this); createItemReq.copyToSalesLine(SalesType); }
class CFSCreateItemReqFrmItemForecast { ForecastSales forecastSales; }
void initParameters(ForecastSales _forecastSales) { forecastSales = _forecastSales; }
public void copyToSalesLine(SalesType _salesType) { ProjTable projTable = ProjTable::find(forecastSales.ProjId); if (_salesType == SalesType::ItemReq) { if (!ProjStatusType::construct(projTable).validateWriteItemRequirement()) { throw error("@SYS18447"); } } else { if (!ProjStatusType::construct(projTable).validateWriteSalesLine()) { throw error("@SYS18447"); } } SalesLine salesLine = this.initializeSalesLine(_salesType, forecastSales, projTable); salesLine.createLine(false, // Validation false, // Init from SalesTable true, // Init from InventTable true, // Calc invent Qty false, // Search markup - copied from salesQuotationline false, // Search price - copied from salesQuotationline false, // Check reservation true); // Skip creditlimit check this.updateSalesLine(salesLine, forecastSales); salesLine.update(); }
protected SalesLine initializeSalesLine(SalesType _salesType, ForecastSales _forecastSales, ProjTable _projTable) { SalesLine salesLine; salesLine.SalesType = _salesType; salesLine.initValue(); salesLine.setInventDimId(_forecastSales.InventDimId); salesLine.ItemId = _forecastSales.ItemId; salesLine.SalesQty = _forecastSales.SalesQty; salesLine.SalesUnit = _forecastSales.SalesUnitId; salesLine.ProjId = _forecastSales.ProjId; salesLine.ActivityNumber = _forecastSales.ActivityNumber; salesLine.CurrencyCode = _forecastSales.Currency; salesLine.initFromProjTable(_projTable, false); return salesLine; }
protected void updateSalesLine(SalesLine _salesLine, ForecastSales _forecastSales) { _salesLine.DefaultDimension = _salesLine.copyDimension(_forecastSales.DefaultDimension); _salesLine.ProjLinePropertyId = _forecastSales.ProjLinePropertyId; _salesLine.TaxGroup = _forecastSales.TaxGroupId; _salesLine.TaxItemGroup = _forecastSales.TaxItemGroupId; _salesLine.ProjCategoryId = _forecastSales.ProjCategoryId; _salesLine.CostPrice = _forecastSales.CostPrice; _salesLine.SalesPrice = _forecastSales.SalesPrice; _salesLine.LinePercent = _forecastSales.DiscPercent; _salesLine.LineDisc = _forecastSales.DiscAmount; _salesLine.LineAmount = 0; _salesLine.LineAmount = _salesLine.calcLineAmount(); SalesLineType_ItemReq::setSalesLineReceiptDate(_salesLine); }