Validate a FlowField Field. Wait? What?

by Dec 18, 2023AL Language

Home 9 Development 9 AL Language 9 Validate a FlowField Field. Wait? What? ( Page 2 )

Validate a FlowField Field. Wait? What?

There are not many things in the AL Language that surprised me. However, last week, I found one such thing – I reviewed customizations made by another partner and had to analyze the OOTB code of the Demand Forecast matrix.

I run a debugger and end up in the “Production Forecast Entry” table in one of the OnAfterValidate triggers added by the custom extension. However, from the static code review, I could not figure out where exactly the code accessed the “Production Forecast Entry” table. When I analyzed the stack trace when the system ran the Validate function against the Item table, it ran the validations against a completely different table – “Production Forecast Entry”).

My first thought was that it was probably too late and I was hallucinating 🙂

So, what is in the base? Everything starts with the following procedure on page 2900 “Demand Forecast Variant Matrix”

Let’s look at the field – the field is a FlowField (a calculated field not stored in the database).

So, what will happen when the code tries to validate a new value against the field that is not stored anywhere? This is where the fun begins.

Undocumented, but has existed since the very first version of Navision

I tried to search for some documentation, but I did not find anything… I would expect at least a few words about this behaviour in one of the following overviews.

However, I remembered reading something on Mibuso about flowfields and data modification a few years back. I could not find the original discussion, but fortunately, I found another discussion with a good explanation from one of our dinosaurs – David Singleton (thanks, David!).

This is standard NAV behaviour, and has been part of Navision since the early DOS versions.

Basically what happens is that NAV looks at the current filters applied to the underlying source Table. It then looks at the sum of the records, and compares this to the number that you just entered. Now NAV creates a new Record in the source table, with a value that will then add up to the new total that you want. It then adds a new record.

For this to work, the Primary key of the source table must end in an integer.

This feature is mainly used for Budgets, in the Base App, but can I use it a lot for many other types of functions.

David Singleton, 10/2007 (yes, it’s really 16 years ago – Validating a flow field?? – Dynamics 365 Business Central/NAV / Developers Forum – Dynamics User Group

So, what if we don’t want to use this approach? How would the explicit code look, which will have the same output as when we validate a flowfield field? Probably similarly to the code below:

procedure ReplaceFlowFieldValidate(ProductionForecastName: Code[20]; ItemNo: Code[20]; VariantCode: Code[10]; ForecastDate: Date; LocationCode: Code[10]; ComponentForecast: Boolean; NewQuantity: Decimal)
var
    ProductionForecastEntry: Record "Production Forecast Entry";
begin
    ProductionForecastEntry.SetRange("Item No.", ItemNo);
    if VariantCode <> '' then
        ProductionForecastEntry.SetRange("Variant Code", VariantCode);
    if ProductionForecastName <> '' then
        ProductionForecastEntry.SetRange("Production Forecast Name", ProductionForecastName);
    if ForecastDate <> 0D then
        ProductionForecastEntry.SetRange("Forecast Date", ForecastDate);
    if LocationCode <> '' then
        ProductionForecastEntry.SetRange("Location Code", LocationCode);
    ProductionForecastEntry.SetRange("Component Forecast", ComponentForecast);

    if not ProductionForecastEntry.IsEmpty() then begin
        ProductionForecastEntry.CalcSums("Forecast Quantity (Base)");
        NewQuantity -= ProductionForecastEntry."Forecast Quantity (Base)";
    end;

    if NewQuantity = 0 then
        exit;

    Clear(ProductionForecastEntry);
    ProductionForecastEntry.Validate(ProductionForecastEntry."Production Forecast Name", ProductionForecastName);
    ProductionForecastEntry.Validate(ProductionForecastEntry."Item No.", ItemNo);
    ProductionForecastEntry.Validate(ProductionForecastEntry."Forecast Date", ForecastDate);
    ProductionForecastEntry.Validate(ProductionForecastEntry."Forecast Quantity", NewQuantity);
    ProductionForecastEntry.Validate(ProductionForecastEntry."Location Code", LocationCode);
    ProductionForecastEntry.Validate(ProductionForecastEntry."Forecast Quantity (Base)", NewQuantity);
    ProductionForecastEntry.Validate(ProductionForecastEntry."Component Forecast", ComponentForecast);
    ProductionForecastEntry.Insert(true);
end;

I believe this can be useful, but it should be described in Microsoft Learn posts about flowfields.

What is your opinion? Have you known about this behaviour? Have you ever used it?

Recent Articles from the category

7 git commands you should know

7 git commands you should know

For many years, developers in C/AL did not need to know anything about Git or other versioning tools. That has changed with Business Central, or more specifically with AL Language. Today, we will look at the most important (and basic) git commands any developer should...

read more
How to define the source for item reservations?

How to define the source for item reservations?

It's not uncommon to have a customer's request to limit from which source items could be reserved. For example, customers may not want to reserve items from return orders. How can we achieve this goal? It's really simple. All the magic is done in the procedure...

read more
NavigationAction for ErrorInfo data type

NavigationAction for ErrorInfo data type

One more article about ErrorInfo data type. Have you already read my previous posts about ErrorInfo and Collectible Errors? ErrorInfo data type & Collectible Errors | MSDyn365 Business Central - Ing. Tomáš Kapitán (kepty.cz) Collectible Errors?! | MSDyn365...

read more
Error actions for ErrorInfo data type

Error actions for ErrorInfo data type

It is already almost one and a half years since the ErrorInfo data type was introduced (we saw this data type for the first time in BC2021w2). If you do not know what it is and how to use this data type, check my previous posts: ErrorInfo data type & Collectible...

read more
Data types under the microscope: List

Data types under the microscope: List

The List data type in AL language represents an ordered collection of objects that can be accessed by their index. Unlike an Array data type, a List does not have a fixed size and does not need to have its dimension specified when it is declared. The List data type...

read more
Connect to Azure Function in BC 2022 wave 2 (v21)

Connect to Azure Function in BC 2022 wave 2 (v21)

The new version of the Microsoft Dynamics 365 Business Central brought a new system module "Azure Functions" that makes integration with Azure Functions much easier and straightforward. The typical scenario, why to use Azure Functions together with Business Central,...

read more

Sign Up for News

Certifications

Highest certification
Microsoft Data Management and
also in D365 Business Central

Microsoft Certified: Dynamics 365 Business Central Functional Consultant Associate

See other certifications here