C/AL + AL: SetAutoCalcFields

by Apr 6, 2019AL Language, C/AL Language

Home 9 Development 9 AL Language 9 C/AL + AL: SetAutoCalcFields

SetAutoCalcFields is very similar method to CalcFields method. The only difference is that CalcField is run on the records already loaded from the database. On the other hand, SetAutoCalcFields is set before the records are loaded from the database.

Although it can be seen as a small difference, it could have a huge impact on performance, especially if the FlowField’s values are used in loops.

Imagine, you want to check the value of the FlowField on each record from the Item table. That will be probably done using FindSet/repeat statements. It can be definitely done using CalcFields in a repeat loop, however, it will generate SQL query for each cycle of repeat statement. So, in case of production environment with 200 000 items, it will generate 200 000 queries just to load the value of the FlowField.

However, if the SetAutoCalcFields method is used, it will generate much less number of queries.

Let’s look on the example

 // Load FlowField using CalcFields method
 if Item.FindSet() then
   repeat
     // CalcFields statement generates SQL query for each Item
     Item.CalcFields(Inventory); 
     Message('Inventory of item no. %1 is %2.', Item."No.", Item.Inventory);
   until Item.Next() < 1; 

 // Now, let's do the same using SetAutoCalcFields
 Item.SetAutoCalcFields(Inventory); 
 if Item.FindSet() then
   repeat
     Message('Inventory of item no. %1 is %2.', Item."No.", Item.Inventory);
   until Item.Next() < 1; 

Recent Articles from the category

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
Collectible Errors?! Is it already in use?

Collectible Errors?! Is it already in use?

Collectible Errors?! Is it already in use? This is the second part of my new article series about Collectible Errors. Let's check out the first part here: Collectible Errors?! | Microsoft Dynamics 365 - Ing. Tomáš Kapitán (kepty.cz) or you might be interested in my...

read more
Collectible Errors?!

Collectible Errors?!

Collectible Errors?! It has been already almost a year since ErrorInfo datatype & CollectibleErrors were introduced (I already have an article about basic structure: ErrorInfo data type & Collectible Errors). This article was released for the first time in...

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