Thanks Bilal, I tried this but it does not work, I think because it is temp table and it perform some calculations based on variables in the page.
That is my code:
procedure GetAverageUnitCost(ItemNo: Code[20])
var
myInt: Integer;
TempAvgCostCalcOverview: Record "Average Cost Calc. Overview" temporary;
AvgCostCalcOverView: Record "Average Cost Calc. Overview";
AvgCostCalcOverviewFilters: Record "Average Cost Calc. Overview";
GetAvgCostCalcOverview: Codeunit "Get Average Cost Calc Overview";
ItemCost: Record "Xee_Item Cost";
begin
Item.get(ItemNo);
TempAvgCostCalcOverview."Item No." := Item."No.";
TempAvgCostCalcOverview.SetFilter("Valuation Date", Item.GetFilter("Date Filter"));
TempAvgCostCalcOverview.SetFilter("Location Code", Item.GetFilter("Location Filter"));
TempAvgCostCalcOverview.SetFilter("Variant Code", Item.GetFilter("Variant Filter"));
// OnInitTempTableOnAfterAvgCostCalcOverviewSetFilters(TempAvgCostCalcOverview, Item);
GetAvgCostCalcOverview.Run(TempAvgCostCalcOverview);
TempAvgCostCalcOverview.Reset();
AvgCostCalcOverviewFilters.CopyFilters(AvgCostCalcOverView);
AvgCostCalcOverView.Reset();
AvgCostCalcOverView.DeleteAll();
if TempAvgCostCalcOverview.Find('-') then
repeat
if TempAvgCostCalcOverview.Level = 0 then begin
AvgCostCalcOverView := TempAvgCostCalcOverview;
AvgCostCalcOverView.Insert();
end;
until TempAvgCostCalcOverview.Next() = 0;
AvgCostCalcOverView.CopyFilters(AvgCostCalcOverviewFilters);
TempAvgCostCalcOverview."Item No." := Item."No.";
TempAvgCostCalcOverview.SetFilter("Valuation Date", Item.GetFilter("Date Filter"));
TempAvgCostCalcOverview.SetFilter("Location Code", Item.GetFilter("Location Filter"));
TempAvgCostCalcOverview.SetFilter("Variant Code", Item.GetFilter("Variant Filter"));
// OnExpandAllOnAfterAvgCostCalcOverviewSetFilters(AvgCostCalcOverview, Item);
GetAvgCostCalcOverview.Run(AvgCostCalcOverview);
AvgCostCalcOverviewFilters.CopyFilters(AvgCostCalcOverView);
AvgCostCalcOverView.Reset();
AvgCostCalcOverView.DeleteAll();
if TempAvgCostCalcOverview.Find('+') then
repeat
AvgCostCalcOverView := AvgCostCalcOverview;
GetAvgCostCalcOverview.Calculate(AvgCostCalcOverview);
TempAvgCostCalcOverview.Reset();
AvgCostCalcOverview := AvgCostCalcOverView;
until TempAvgCostCalcOverview.Next(-1) = 0;
if TempAvgCostCalcOverview.Find('+') then
repeat
AvgCostCalcOverView := AvgCostCalcOverview;
AvgCostCalcOverView.Insert();
until TempAvgCostCalcOverview.Next(-1) = 0;
AvgCostCalcOverView.CopyFilters(AvgCostCalcOverviewFilters);
AvgCostCalcOverView.Reset();
AvgCostCalcOverView.SetCurrentKey("Attached to Valuation Date", "Attached to Entry No.", Type);
AvgCostCalcOverView.SetFilter("Item No.", Item."No.");
ItemName := StrSubstNo('%1 %2', Item."No.", Item.Description);
if AvgCostCalcOverView.FindFirst() then
repeat
DocumentLineNoHideValue := false;
EntryTypeHideValue := false;
ItemLedgerEntryNoHideValue := false;
TypeIndent := 0;
// SetExpansionStatus();
if AvgCostCalcOverView.Type = AvgCostCalcOverView.Type::"Closing Entry" then begin
AvgCostCalcOverView.Quantity := AvgCostCalcOverView.CalculateRemainingQty();
AvgCostCalcOverView."Cost Amount (Expected)" := AvgCostCalcOverView.CalculateCostAmt(false);
AvgCostCalcOverView."Cost Amount (Actual)" := AvgCostCalcOverView.CalculateCostAmt(true);
AvgCostCalcOverView."Cost Amount (Market)" := AvgCostCalcOverView.CalculateCostAmtACY(true);
ItemCost.SetRange("Item No", AvgCostCalcOverView."Item No.");
ItemCost.SetRange("Valuation Date", AvgCostCalcOverView."Valuation Date");
if ItemCost.FindFirst() then begin
if AvgCostCalcOverView.Quantity <> 0 then
ItemCost."Cost Unit ACY" := AvgCostCalcOverView."Cost Amount (Market)" / AvgCostCalcOverView.Quantity;
// else
ItemCost.Modify();
// CurrPage.Close();
// CurrPage.Close();
end
else begin
ItemCost.Init();
ItemCost."Item No" := AvgCostCalcOverView."Item No.";
ItemCost."Valuation Date" := AvgCostCalcOverView."Valuation Date";
ItemCost."Cost Unit ACY" := AvgCostCalcOverView."Cost Amount (Market)" / AvgCostCalcOverView.Quantity;
ItemCost.Insert(true);
// CurrPage.Close();
end;
end;
// TypeOnFormat();
// ItemLedgerEntryNoOnFormat();
// EntryTypeOnFormat();
// DocumentLineNoOnFormat();
until AvgCostCalcOverView.next = 0;
end;