Manipulate Business Central records safely
Record variables connect AL logic to Business Central tables. Safe data access depends on choosing methods that match the expected cardinality and preserve the table's business rules.
Retrieve one record or a set
Use Get when you know the complete primary key and expect at most one record. Consume its Boolean return value when a missing record is an expected outcome. Call it without consuming the return value when the missing record should raise an error.
if not Item.Get(ItemNo) then
Error(ItemNotFoundErr, ItemNo);
Use SetRange for a simple inclusive filter and SetFilter for expressions such as alternatives, comparisons, and wildcards. Filters belong to the record variable, so keep each record variable's purpose clear.
Use FindFirst or FindLast when you need one boundary record. Use FindSet when you intend to repeat through the filtered set. FindSet retrieves a set for efficient iteration, while repeated calls to other find methods can create unnecessary requests.
Item.SetRange(Blocked, false);
if Item.FindSet() then
repeat
InspectItem(Item);
until Item.Next() = 0;
Preserve field and table behavior
Direct assignment changes a field value in memory. Validate changes the value and runs the field's validation logic. Use Validate when you want the same table rules and related updates that apply during normal data entry.
Item.Validate("Item Review Priority", ReviewPriority);
Item.Validate("Next Review Date", ReviewDate);
Item.Modify(true);
The true parameter on Insert(true), Modify(true), and Delete(true) runs the corresponding table trigger. Omitting it or passing false bypasses that trigger. Bypassing triggers is appropriate only when the design explicitly owns all required invariants.
Don't move essential validation only to a page. Pages are one way to call the data model, but APIs, background sessions, reports, and codeunits can modify the same table. Put shared data integrity rules on the table or in a domain service that every caller uses.
Plan transactions and concurrency
AL database changes run within a transaction until the runtime commits or rolls back the work. An unhandled error rolls back the active transaction. Avoid Commit() in reusable processing unless the business process requires an explicit transaction boundary because a commit prevents later errors from rolling back earlier changes.
Concurrent sessions can read and update the same data. Use LockTable or an appropriate ReadIsolation setting only when the process requires stronger consistency, and keep locked work short. Re-read critical values before a write when another session can change them.
For an import, validate the row first, retrieve the target record, call Validate for field changes, and then call Modify(true). This sequence keeps parsing failures separate from database writes and applies the target table's rules.
For more information, see Record data type and AL database methods and performance.