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

BC Open Source? How to start?

BC Open Source? How to start?

BC Open Source? How to start? One of the most exciting news introduced last month in Lyon during Directions EMEA 2023 was the changes to the open-source initiative. This means that you can now contribute to the source code of the Base app and the System app, which are...

read more
Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What?

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

read more
Dynamics NAV 2013 & Expired Cronus License

Dynamics NAV 2013 & Expired Cronus License

We found an interesting problem - we were not able to run the development environment for Dynamics NAV 2013. Whenever we tried to run the development client, we got the following error message: "Your program license has expired" and the development client has closed...

read more
Indirect Dependencies and Access Modifiers

Indirect Dependencies and Access Modifiers

Last week, there was a discussion on Yammer on how to get values from the "Sent Email" record when all fields are marked as Internal. I was surprised that many people do not know what can/can't access modifiers (such as local, protected, or internal) be used for. I...

read more
AL Extensions: Replace Document Attachment

AL Extensions: Replace Document Attachment

I have published a new simple, open-source extension that allows replacing existing document attachments in all master entities as well as in open documents. The source code as well as the app file that can be installed in your environment is available on my GitHub...

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