Init Code
Init code is a block of AL procedures that belongs to the sheet. It is compiled and run fresh on every recalculation, and every procedure it declares becomes callable from any cell formula — real AL, with record variables, filters and CalcSums.
Open it with Sheet → Init Code… in the editor. The code opens in a full AL source editor with syntax highlighting, completion (records, fields, built-in functions — plus the engine's glsum, glbudget and tablesum record functions) and parameter hints while typing inside a call. OK keeps the changes, Cancel discards them.
Example
procedure SafeDiv(Num: Decimal; Den: Decimal): Decimal
begin
if Den = 0 then
exit(0);
exit(Num / Den);
end;
procedure MonthRevenue(DateFlt: Text; ItemFlt: Text): Decimal
var
VE: Record "Value Entry";
begin
VE.SetRange("Item Ledger Entry Type", VE."Item Ledger Entry Type"::Sale);
VE.SetFilter("Posting Date", DateFlt);
if ItemFlt <> '' then
VE.SetFilter("Item No.", ItemFlt);
VE.CalcSums("Sales Amount (Actual)");
exit(VE."Sales Amount (Actual)");
end;
Cells then call them like built-ins:
=SafeDiv(B4, B5)
=MonthRevenue('2026-01-01..2026-01-31', E2)
=MonthRevenue(C2, E2)
Pass slicer values as plain cell references (C2), not as [C2] — the [Address] token belongs to region filters and SQL statements, and is not substituted inside formulas. A slicer cell's value arrives as text (or a number when it parses as one), ready for SetFilter.
When to Use Init Code
- Aggregations the sheet functions cannot express — sums over tables with several filters, ratios of two sums, anything needing AL logic.
- Guard procedures like
SafeDiv, so a ratio never divides by zero. - Shared business logic used by many cells — one procedure instead of a repeated mega-formula.
For simple filtered sums, prefer the cheaper alternatives first: GL.GLSum for G/L amounts, or a feeder table region read with TableSum (see Table Regions).
Behaviour
- Procedures are recompiled and re-registered on every recalculation, so editing the code takes effect immediately.
- Compile or runtime errors are reported on the sheet (as an init-code error message) without blocking the rest of the sheet.
- Declared procedures appear in the formula IntelliSense.
- Init code travels with templates: applying a template merges its procedures into the sheet's init code, skipping names the sheet already declares, and never removing anything. See Templates.