Skip to main content

Notifications

Announcements

No record found.

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,642 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,371 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans