Use partial records on read operation (LC0095)
When a local record variable performs a read operation (Get, Find, FindFirst, FindLast, or FindSet) without a preceding SetLoadFields call, the runtime loads all normal fields from the table, including fields added by table extensions. Each table extension introduces an additional SQL join, which can degrade performance by 2-9x depending on the number of extensions installed.
Calling SetLoadFields before a read tells the runtime to load only the fields you actually need. This is known as using partial records.
A code fix is available for this diagnostic.
Example
The following code reads a full record when only one field is needed:
codeunit 50100 MyCodeunit
{
procedure GetCustomerName(CustomerNo: Code[20]): Text
var
Customer: Record Customer;
begin
Customer.Get(CustomerNo);
exit(Customer.Name);
end;
}To fix this, add SetLoadFields before the read operation:
codeunit 50100 MyCodeunit
{
procedure GetCustomerName(CustomerNo: Code[20]): Text
var
Customer: Record Customer;
begin
Customer.SetLoadFields(Customer.Name);
Customer.Get(CustomerNo);
exit(Customer.Name);
end;
}
When the rule does not trigger
The rule suppresses the diagnostic when any of the following conditions apply:
SetLoadFields,AddLoadFields, orSetBaseLoadFieldsis already called on the variable- The variable is used in a write operation (
Insert,Modify,Delete,Rename,TransferFields,Init,Copy, etc.) - The variable is passed as an argument to any function, event, or
PAGE.Run/PAGE.RunModal - The variable is a temporary record
- The variable is a global or parameter variable (only local variables are analyzed)
- The variable is a
RecordRef(analyzed for the diagnostic, but the code fix is not offered since field numbers are not statically determinable)
Code fix
The ALCops: Add SetLoadFields code fix inserts a SetLoadFields call on the line before the read operation. It inspects the method body for field accesses on the variable and pre-populates the argument list with those fields, sorted alphabetically. When no field accesses are found, it falls back to the primary key fields.
FlowField, FlowFilter, and Blob fields are excluded because SetLoadFields does not support them.
Relationship to AA0242
This rule complements the CodeCop rule AA0242 (“Field is not selected for loading and accessing it may cause a JIT load”). The two rules cover different scenarios:
| Rule | When it fires |
|---|---|
| LC0095 | SetLoadFields is entirely absent before a read operation |
| AA0242 | SetLoadFields is present but a field access is missing from the field list |
Together they provide full coverage: LC0095 encourages adopting partial records, and AA0242 ensures the field list stays complete.
See also
- Using Partial Records on Microsoft Learn
- Record.SetLoadFields on Microsoft Learn
- RecordRef.SetLoadFields on Microsoft Learn
- SetLoadFields best practices on ALGuidelines.dev