When building customizations or integrations in Microsoft Dynamics 365 Business Central, performance is often a key concern—especially when working with large datasets. One powerful tool for improving performance is the SETLOADFIELDS
method on record variables.
What is
SETLOADFIELDS
?
In Business Central, when you retrieve a record (using FIND
, GET
, NEXT
, etc.), all fields of the record are loaded from the database by default. This includes fields you might never use, such as FlowFields (which are calculated on the fly). This can lead to unnecessary memory usage and slower data retrieval.
SETLOADFIELDS
tells the system which fields to load, so it skips the rest. This can dramatically improve performance, particularly when:
Syntax
Rec.SETLOADFIELDS(Field1, Field2, ...);
You place this line before reading the records. This tells the AL runtime engine to load only the specified fields when the records are retrieved.
Let’s say you need to loop through all items and check their No.
and Inventory
:
Item.SETLOADFIELDS("No.", Inventory);
if Item.FINDFIRST then
repeat
if Item.Inventory > 0 then
// do something
until Item.NEXT = 0;
SETLOADFIELDS
only when needed – Overusing it without understanding the data flow may result in missing fields or unexpected behavior.CALCFIELDS
still works – You can still explicitly calculate FlowFields after using SETLOADFIELDS
.MODIFY
, INSERT
, or VALIDATE
, the load fields context is reset. Use SETLOADFIELDS
again if necessary.SETLOADFIELDS
helps you skip them efficiently.Feature | Without SETLOADFIELDS | With SETLOADFIELDS |
---|---|---|
Data Loaded | All fields | Only specified fields |
Memory Usage | High | Lower |
Speed | Slower | Faster |
By leveraging SETLOADFIELDS
, developers can significantly optimize the performance of their AL code in Business Central. It’s a small addition that can make a big difference in speed and scalability.
Avoid it when doing full record operations like MODIFY
, unless you’re confident about which fields are required.
Stay Tuned for more…
Original Post https://ammolhsaallvi.blog/2025/05/06/boosting-performance-in-business-central-with-setloadfields/