Do not use partial records before write operations (PC0031)
When SetLoadFields, AddLoadFields, or SetBaseLoadFields is used on a record variable that subsequently performs write operations (Insert, Modify, Delete, Rename, TransferFields, or Copy), the platform must JIT-load all remaining fields before the write can execute.
This causes two problems:
- Performance penalty: The JIT load triggers an extra SQL roundtrip, making the code strictly slower than not using partial records at all (2 roundtrips instead of 1).
- Runtime errors: Under concurrent access, the JIT load can fail with “Inconsistent read of field(s)” (when another user modifies the record between the initial load and the JIT load) or “JIT loading of field(s) failed” (when the record is deleted or renamed).
A code fix is available for this diagnostic.
Example
The following code uses SetLoadFields before a Modify operation, causing a JIT load:
codeunit 50100 MyCodeunit
{
procedure UpdateItemDescription(ItemNo: Code[20])
var
Item: Record Item;
begin
Item.SetLoadFields(Item."No."); // PC0031: Do not use 'SetLoadFields' before write operations (Modify) on 'Item'.
Item.Get(ItemNo);
Item.Description := 'Updated';
Item.Modify();
end;
}
table 50100 Item
{
fields
{
field(1; "No."; Code[20]) { }
field(2; Description; Text[100]) { }
}
keys
{
key(PK; "No.") { }
}
}
To fix this, remove the SetLoadFields call when the record will be written:
codeunit 50100 MyCodeunit
{
procedure UpdateItemDescription(ItemNo: Code[20])
var
Item: Record Item;
begin
Item.Get(ItemNo);
Item.Description := 'Updated';
Item.Modify();
end;
}
table 50100 Item
{
fields
{
field(1; "No."; Code[20]) { }
field(2; Description; Text[100]) { }
}
keys
{
key(PK; "No.") { }
}
}
Scenarios that are not flagged
- When the record variable only has read operations after
SetLoadFields(use PC0030 instead) - When
ModifyAll,DeleteAll, orInitare the only write operations (set-based or initialization operations that don’t trigger JIT loads) - When
Clear()orReset()is called betweenSetLoadFieldsand the write operation (resets the partial records state) - On temporary record variables (no SQL backing, no JIT load)
Code fix
The code fix removes the SetLoadFields, AddLoadFields, or SetBaseLoadFields statement. Since the record will be written, loading all fields upfront is the correct behavior.