web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

how to execute the code Onopen page and on OnAfterGetRecordin an event subscriber

(0) ShareShare
ReportReport
Posted on by 39

I have a page that calculates the unit cost of item (OnAfterGetRecord) and I want to execute that code when value entry is inserted. I tried page.run on the following events  OnAfterInsertValueEntry and OnBeforeInsertValueEntry, but it runs the code after finishing the event`s code ( and the entire codeunit). is there any wat i can execute the code (OnAfterGetRecord) inside the event ?

I have the same question (0)
  • Suggested answer
    Bilal Haider Profile Picture
    442 on at

    Hi Muaaz,

    You should copy the code that calculates unit cost and put it in subscriber function.

  • Muaaz Deyab Profile Picture
    39 on at

    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;

  • Suggested answer
    Bilal Haider Profile Picture
    442 on at

    Hi,

    In your code there are filters that might be missing, make sure you are passing all the required information for calculation.

  • Suggested answer
    Muaaz Deyab Profile Picture
    39 on at

    I solved this using the codeunit ItemCostmanagment

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,958 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,101 Super User 2026 Season 1

#3
Dhiren Nagar Profile Picture

Dhiren Nagar 959 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans