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 29

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 ?

  • Suggested answer
    Muaaz Deyab Profile Picture
    Muaaz Deyab 29 on at
    RE: how to execute the code Onopen page and on OnAfterGetRecordin an event subscriber

    I solved this using the codeunit ItemCostmanagment

  • Suggested answer
    Bilal Haider Profile Picture
    Bilal Haider 232 on at
    RE: how to execute the code Onopen page and on OnAfterGetRecordin an event subscriber

    Hi,

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

  • Muaaz Deyab Profile Picture
    Muaaz Deyab 29 on at
    RE: how to execute the code Onopen page and on OnAfterGetRecordin an event subscriber

    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
    Bilal Haider 232 on at
    RE: how to execute the code Onopen page and on OnAfterGetRecordin an event subscriber

    Hi Muaaz,

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

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,407 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans