Search code examples
saasnavisiondynamics-navdynamics-business-centralmicrosoft-dynamics-nav

How to get the value of a Business Central Page field based on a global variable


In the Demand Forecast page 99000919, all of the "Header" data fields are backed by global variables. I need to default the View by Period (PeriodType) to Month and then validate the value. I have not been successful at doing so.

A. Using Modify(PeriodType) to get the value from the OnAfterValidate trigger. PeriodType shows up in intellisense when I use the Modify, but when I try to use it, I get a compiler error that PeriodType does not exist in the current context.

    modify(PeriodType)
    {
    trigger OnAfterValidate()
    begin
        MyPeriodType := PeriodType;
    end;
    }

B. I also tried to use the VS Code teventsub code snippet to subscribe to the OnAfterValidate event for PeriodType, but there is nothing exposed.

    [EventSubscriber(ObjectType::Page, Page::"Demand Forecast", 'OnBeforeValidateEvent', 'PeriodType', true, true)]

Can this be done in the SaaS solution with extensions? Any ideas or suggestions are welcomed.


Solution

  • At the moment there does not appear to be any events for you to subscribe to, that would help you solve the problem.

    The first thing to do then is to submit a request to Microsoft to add the needed events to the product. Request New Events

    That being said there is a workaround for your problem, which involves a few steps:

    1. Create a copy of Demand Forecast (Page 99000919).
    2. Code your required changes in your copy of the page
    3. Substitute the original page with your copy

    The last step is achieved by identifying all places where the original page is opened and then overtaking the code.

    There are three places where Demand Forecast is opened:

    1. Page 99000921, Demand Forecast Names, Action Edit Demand Forecast
    2. Page 8907, Sales & Marketing Manager RC, Action Forecast
    3. Codeunit 5530, Calc. Item Availability

    1 and 2 are run non-modal which means in both cases you can do the following:

    [EventSubscriber(ObjectType::Page, Page::"Demand Forecast Names", 'OnBeforeActionEvent', 'Edit Demand Forecast', true, true)]
    local procedure Page_DemandForecastNames_OnBeforeActionEvent_EditDemandForecast(var Rec: Record "Production Forecast Name")
    var
        DemandForecastCopy: Page "Demand Forecast"; // this variable should reference your copy
    begin
        DemandForecastCopy.SetProductionForecastName(Rec.Name);
        DemandForecastCopy.Run();
        Error(''); // this creates a silent error which cancels the original execution of the action.
    end;
    

    SetProductionForecastName should not be called for "Sales & Marketing Manager RC".

    The third one is a bit trickier because the page is run modal from the codeunit, but since we have now replaced all occurrences where the page is run non-modal, we can now assume that every time the original Demand Forecast is opened it should be modal.

    Unfortunately subscribing to the Demand Forecast OnOpenPageEvent won't help you because there are no parameters to get values from. So you have to backtrack the code from Calc. Item Availability to find all possible entry points and then override the default implementation as described earlier.