In this post I´ll show seven basic tips for increasing performance when coding in Business Central.
Some of them are well-known as they have been part of C/AL for a long time. And others like partial records were introduced recently.
At the end of the post, you will find three Youtube links in which you can dig into more detail about the topics presented.
- FINDSET VS FINDFIRST
- PARTIAL RECORDS. SETLOADFIELDS
- LAZY EVALUATION
FINDSET VS FINDFIRST
Use FindSet instead of FindFirst or FindLast when you wish to loop through a series of records.
Also, use FindSet(true) when you wish to modify any record in the set, otherwise use FindSet(false).
Only use FindFirst() when you wish to retrieve one record and a loop isn´t needed.
PARTIAL RECORDS. SETLOADFIELDS
SetLoadFields only loads the fields included. Not retrieving the rest of the fields in the table and providing a faster load. Use it to look for specific values as in the example below.
Don´t use it if you need to perform a modify, insert, delete, rename, or transferfield of the record as all fields are needed to perform the operations. This will decrease performance and will be counter-productive.
You can improve performance by using the SetAutoCalcFields function before looping through records with FlowFields instead of calling CalcFields on each record in the loop.
Use CalcSums instead of looping through all the records in the table to perform the sum of a specific field or fields. In this example, both pieces of code achieve the same result but the last one is better for performance.
The ‘lazy evaluation’ is a strategy that holds the evaluation of an expression until its value is needed. This method is not applicable in AL.
The idea is that when we need to evaluate two conditions, always place the fastest first and separate from the other. This way the more intensive operation isn´t executed if it´s not needed.
You will find more information about it in Stefano´s webinar at the link at the end.
Use IsEmpty if you just want to know if a record exists but don´t need to know any values.
For example, when using DeleteAll, even if there is nothing to delete the system will still perform a lock. So it´s better to first check if it´s empty and avoid the lock when possible.
Selects a key for the table. It sorts the data of this specific table and allows better performance for large datasets.
Stefano´s webinar at DynamicsCon. I strongly recommend checking it as it has several tips not only for coding but for codeunits, pages, etc.
Tomas´s webinar at DynamicsCon. Very interesting as he focuses on data structure, keys, performance tests, etc.
Business Central Channel. Overall good video for performance tips.
Hope you find it useful.