From 9093c1407bb414ac6a2fdddd9b4b6065e0901a90 Mon Sep 17 00:00:00 2001 From: Stefan Sosic <59221826+StefanSosic@users.noreply.github.com> Date: Mon, 12 May 2025 20:57:08 +0200 Subject: [PATCH 1/4] Add documentation for SetAutoCalcFields method --- .../BestPractices/SetAutoCalcFields/index.md | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 content/docs/BestPractices/SetAutoCalcFields/index.md diff --git a/content/docs/BestPractices/SetAutoCalcFields/index.md b/content/docs/BestPractices/SetAutoCalcFields/index.md new file mode 100644 index 00000000..b98d9475 --- /dev/null +++ b/content/docs/BestPractices/SetAutoCalcFields/index.md @@ -0,0 +1,64 @@ +--- +title: "SetAutoCalcFields" +tags: ["AL","Performance"] +categories: ["Best Practice"] +--- + +In Business Central, `SetAutoCalcFields` is an essential method for optimizing the retrieval of calculated fields, such as FlowFields. Without `SetAutoCalcFields`, FlowFields require explicit calculation via `CalcFields`, which adds performance overhead. By automatically computing FlowFields upon retrieval, `SetAutoCalcFields` reduces unnecessary calls and improves efficiency when reading FlowFields from multiple records in a loop. + +See the documentation on learn.microsoft.com for more information about [SetAutoCalcFields](https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/record/record-setautocalcfields-method). + +{{% alert title="Note" color="info" %}} +Starting in Business Central 2025 release wave 1, the SetAutoCalcFields is also available on the RecordRef data type. +{{% /alert %}} + +## Why Use SetAutoCalcFields? + +Using `SetAutoCalcFields` ensures that FlowFields are computed automatically when retrieving a record, eliminating the need for additional `CalcFields` calls for each record. + +## General Guidelines for SetAutoCalcFields + +- Use `SetAutoCalcFields` before retrieving records (`FindSet`, `Find('-')`, etc.) when FlowFields need to be accessed. +- Avoid redundant `CalcFields` calls if `SetAutoCalcFields` has already been used. + + +## Examples +### Bad Code + +```AL +Customer.SetFilter(Balance, '>%1' , LargeCredit); +if Customer.FindSet() then + repeat + Customer.CalcFields(Balance); + if Customer.Balance > MaxCreditLimit then begin + Customer.Blocked := true; + Customer.Modify(); + end else + if Customer.Balance > LargeCredit then begin + Customer.Caution := true; + Customer.Modify(); + end; + until Customer.Next() = 0; +``` + +In this example, an extra call to `CalcFields` still must be issued for the code to be able to check the value of `Customer.Balance`. You can optimize this further by using the new `SetAutoCalcFields` method. + +### Good code + +```AL +Customer.SetFilter(Balance, '>%1', LargeCredit); +Customer.SetAutoCalcFields(Balance) +if Customer.FindSet() then + repeat + if Customer.Balance > MaxCreditLimit then begin + Customer.Blocked := true; + Customer.Modify(); + end else + if Customer.Balance > LargeCredit then begin + Customer.Caution := true; + Customer.Modify(); + end; + until Customer.Next() = 0; +``` + + From 77fa3d8f7fad2b5ac8f23722aa1301ed6ead388a Mon Sep 17 00:00:00 2001 From: Stefan Sosic <59221826+StefanSosic@users.noreply.github.com> Date: Thu, 29 May 2025 22:49:58 +0200 Subject: [PATCH 2/4] PR Suggestion Addition --- content/docs/BestPractices/SetAutoCalcFields/index.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/content/docs/BestPractices/SetAutoCalcFields/index.md b/content/docs/BestPractices/SetAutoCalcFields/index.md index b98d9475..80aa56e8 100644 --- a/content/docs/BestPractices/SetAutoCalcFields/index.md +++ b/content/docs/BestPractices/SetAutoCalcFields/index.md @@ -21,6 +21,12 @@ Using `SetAutoCalcFields` ensures that FlowFields are computed automatically whe - Use `SetAutoCalcFields` before retrieving records (`FindSet`, `Find('-')`, etc.) when FlowFields need to be accessed. - Avoid redundant `CalcFields` calls if `SetAutoCalcFields` has already been used. +## SetAutoCalcFields on List Pages +SetAutoCalcFields can be utilized in the OnOpenPage trigger to ensure specific FlowFields are pre-calculated before the user interacts with the page. This is particularly beneficial for list pages where calculations are necessary but not included in the visible fields. However, it's important to note that any FlowField already displayed on the page will be calculated automatically, making SetAutoCalcFields unnecessary for those fields. + +## SetAutoCalcFields or CalcFields +It's best practice to use SetAutoCalcFields when fetching a record since it ensures automatic FlowField calculation. On the other hand, CalcFields should only be used if you already have the record and need to explicitly trigger the calculation. This distinction prevents unnecessary performance overhead, ensuring that database queries remain optimized. + ## Examples ### Bad Code From 02860bd53ff0afe625a8f354b469900cb2e0487c Mon Sep 17 00:00:00 2001 From: Stefan Sosic <59221826+StefanSosic@users.noreply.github.com> Date: Wed, 4 Jun 2025 00:03:07 +0200 Subject: [PATCH 3/4] Add created and described by --- content/docs/BestPractices/SetAutoCalcFields/index.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/content/docs/BestPractices/SetAutoCalcFields/index.md b/content/docs/BestPractices/SetAutoCalcFields/index.md index 80aa56e8..9df9893f 100644 --- a/content/docs/BestPractices/SetAutoCalcFields/index.md +++ b/content/docs/BestPractices/SetAutoCalcFields/index.md @@ -4,6 +4,8 @@ tags: ["AL","Performance"] categories: ["Best Practice"] --- +_Created by SosicStefan, Described by SosicStefan_ + In Business Central, `SetAutoCalcFields` is an essential method for optimizing the retrieval of calculated fields, such as FlowFields. Without `SetAutoCalcFields`, FlowFields require explicit calculation via `CalcFields`, which adds performance overhead. By automatically computing FlowFields upon retrieval, `SetAutoCalcFields` reduces unnecessary calls and improves efficiency when reading FlowFields from multiple records in a loop. See the documentation on learn.microsoft.com for more information about [SetAutoCalcFields](https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/record/record-setautocalcfields-method). From c67356cb8b363b083f36f2e7480f606374a6328d Mon Sep 17 00:00:00 2001 From: Stefan Sosic <59221826+StefanSosic@users.noreply.github.com> Date: Wed, 4 Jun 2025 00:03:28 +0200 Subject: [PATCH 4/4] Description header --- content/docs/BestPractices/SetAutoCalcFields/index.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/content/docs/BestPractices/SetAutoCalcFields/index.md b/content/docs/BestPractices/SetAutoCalcFields/index.md index 9df9893f..bfb647d7 100644 --- a/content/docs/BestPractices/SetAutoCalcFields/index.md +++ b/content/docs/BestPractices/SetAutoCalcFields/index.md @@ -6,6 +6,8 @@ categories: ["Best Practice"] _Created by SosicStefan, Described by SosicStefan_ +## Description + In Business Central, `SetAutoCalcFields` is an essential method for optimizing the retrieval of calculated fields, such as FlowFields. Without `SetAutoCalcFields`, FlowFields require explicit calculation via `CalcFields`, which adds performance overhead. By automatically computing FlowFields upon retrieval, `SetAutoCalcFields` reduces unnecessary calls and improves efficiency when reading FlowFields from multiple records in a loop. See the documentation on learn.microsoft.com for more information about [SetAutoCalcFields](https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/record/record-setautocalcfields-method).