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

Notifications

Announcements

No record found.

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
    412 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
    412 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,990

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,576 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,028 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans