Create Item Requirement from Item Forecast using X++ in D365 Operations


In this blog article, we will see how we can create Item Requirement on insertion of Item Forecast using code.


  1. 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.
  2. public void insert()
        next insert();
        SalesType salesType = SalesType::ItemReq;
        CFSCreateItemReqFrmItemForecast createItemReq = new CFSCreateItemReqFrmItemForecast();
  3. Create a new class that will initialize values and insert record in Item Requirement form.
  4. class CFSCreateItemReqFrmItemForecast
        ForecastSales forecastSales;
  5.  In the new class create a method initParameter. This method will initialize ForecastSales object.
  6. void initParameters(ForecastSales _forecastSales)
        forecastSales = _forecastSales;
  7.  Create another method ‘copytoSalesLine’. It will validate the record and call other methods to copy values to SalesLine Table.
  8. public void copyToSalesLine(SalesType _salesType)
        ProjTable projTable =   ProjTable::find(forecastSales.ProjId);
        if (_salesType == SalesType::ItemReq)
            if (!ProjStatusType::construct(projTable).validateWriteItemRequirement())
                throw error("@SYS18447");
            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);
  9. Create a new method ‘initializeSalesLine’. It is called from copyToSalesLine().
  10. 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;
  11. Create a new method updateSalesLine(). It is called from copyToSalesLine() method.
  12. 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();

Share Story :