Exercise - Implement a codeunit-based processing service
In this exercise, you add a small processing service to an existing AL project that already connects to a Business Central sandbox. Every object uses extension-owned data and an ID from 70500 through 70599.
Add the processing data and page
Create ProcessingEntry.Table.al with the entry, status enum, and processing-mode field.
enum 70500 "Entry Process Status"
{
Extensible = false;
value(0; Open) { Caption = 'Open'; }
value(1; Ready) { Caption = 'Ready'; }
value(2; Completed) { Caption = 'Completed'; }
value(3; Failed) { Caption = 'Failed'; }
}
table 70500 "Processing Entry"
{
Caption = 'Processing Entry';
DataClassification = CustomerContent;
fields
{
field(1; "Entry No."; Integer)
{
AutoIncrement = true;
}
field(2; Description; Text[100]) { }
field(3; "Process Mode"; Enum "Entry Process Mode") { }
field(4; Status; Enum "Entry Process Status") { }
field(5; Result; Text[100]) { }
field(6; "Event Note"; Text[100]) { }
field(7; "Force Failure"; Boolean) { }
field(8; "Task ID"; Guid) { }
field(9; "Processed At"; DateTime) { }
}
keys
{
key(PK; "Entry No.")
{
Clustered = true;
}
}
}
Create ProcessingEntries.Page.al. The page action commits the entry before the scheduler creates a ready task. A Message is valid here because the page action runs in the interactive session, not in the scheduled task.
page 70500 "Processing Entries"
{
PageType = List;
SourceTable = "Processing Entry";
ApplicationArea = All;
UsageCategory = Tasks;
Caption = 'Processing Entries';
layout
{
area(Content)
{
repeater(Entries)
{
field("Entry No."; Rec."Entry No.") { Editable = false; }
field(Description; Rec.Description) { }
field("Process Mode"; Rec."Process Mode") { }
field(Status; Rec.Status) { Editable = false; }
field(Result; Rec.Result) { Editable = false; }
field("Event Note"; Rec."Event Note") { Editable = false; }
field("Force Failure"; Rec."Force Failure") { }
field("Task ID"; Rec."Task ID") { Editable = false; }
field("Processed At"; Rec."Processed At") { Editable = false; }
}
}
}
actions
{
area(Processing)
{
action(ScheduleProcessing)
{
Caption = 'Schedule processing';
ApplicationArea = All;
Image = Schedule;
trigger OnAction()
var
Scheduler: Codeunit "Entry Task Scheduler";
begin
Scheduler.Schedule(Rec);
CurrPage.Update(false);
Message('Task %1 is ready for entry %2.', Rec."Task ID", Rec."Entry No.");
end;
}
}
}
}
Add the strategy and service
Create EntryProcessor.Interface.al with the contract, enum selection, and three implementations. The standard and priority values produce different visible results. The unknown implementation reports an unavailable stored value instead of silently selecting a strategy.
interface "Entry Processor"
{
procedure GetResult(Entry: Record "Processing Entry"): Text[100];
}
enum 70501 "Entry Process Mode" implements "Entry Processor"
{
Extensible = true;
DefaultImplementation = "Entry Processor" = "Standard Entry Processor";
UnknownValueImplementation = "Entry Processor" = "Unknown Entry Processor";
value(0; Standard)
{
Caption = 'Standard';
Implementation = "Entry Processor" = "Standard Entry Processor";
}
value(1; Priority)
{
Caption = 'Priority';
Implementation = "Entry Processor" = "Priority Entry Processor";
}
}
codeunit 70503 "Standard Entry Processor" implements "Entry Processor"
{
procedure GetResult(Entry: Record "Processing Entry"): Text[100]
begin
exit(StrSubstNo('Standard result: %1', Entry.Description));
end;
}
codeunit 70504 "Priority Entry Processor" implements "Entry Processor"
{
procedure GetResult(Entry: Record "Processing Entry"): Text[100]
begin
exit(StrSubstNo('Priority result: %1', UpperCase(Entry.Description)));
end;
}
codeunit 70505 "Unknown Entry Processor" implements "Entry Processor"
{
procedure GetResult(Entry: Record "Processing Entry"): Text[100]
begin
Error('The processing mode on entry %1 is unavailable.', Entry."Entry No.");
end;
}
Create EntryProcessService.Codeunit.al. The service makes a direct interface call, changes extension-owned fields, and raises an integration event. The subscriber writes an observable note in the same transaction.
codeunit 70500 "Entry Process Service"
{
procedure Process(var Entry: Record "Processing Entry")
var
Processor: Interface "Entry Processor";
begin
Entry.TestField(Status, Entry.Status::Ready);
if Entry."Force Failure" then
Error('Controlled processing failure for entry %1.', Entry."Entry No.");
Processor := Entry."Process Mode";
Entry.Result := CopyStr(Processor.GetResult(Entry), 1, MaxStrLen(Entry.Result));
Entry.Status := Entry.Status::Completed;
Entry."Processed At" := CurrentDateTime;
Entry.Modify(true);
OnAfterEntryProcessed(Entry);
end;
[IntegrationEvent(false, false)]
local procedure OnAfterEntryProcessed(var Entry: Record "Processing Entry")
begin
end;
}
codeunit 70501 "Entry Event Subscriber"
{
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Entry Process Service", 'OnAfterEntryProcessed', '', false, false)]
local procedure RecordProcessingNote(var Entry: Record "Processing Entry")
begin
Entry."Event Note" := CopyStr(
StrSubstNo('Subscriber observed %1 mode.', Entry."Process Mode"),
1,
MaxStrLen(Entry."Event Note"));
Entry.Modify(true);
end;
}
Add lifecycle and scheduled-task code
Create EntryLifecycle.Codeunit.al. Installation inserts one sample entry only on first install and registers the migration tag for fresh data. The upgrade phases check prerequisites, apply a tagged repeatable change, and validate completion.
codeunit 70506 "Entry Install"
{
Subtype = Install;
trigger OnInstallAppPerCompany()
var
Module: ModuleInfo;
Entry: Record "Processing Entry";
begin
NavApp.GetCurrentModuleInfo(Module);
if Module.DataVersion = Version.Create(0, 0, 0, 0) then begin
if Entry.IsEmpty() then begin
Entry.Init();
Entry.Description := 'Sample processing entry';
Entry."Process Mode" := Entry."Process Mode"::Standard;
Entry.Status := Entry.Status::Open;
Entry.Insert(true);
end;
RegisterUpgradeTag();
end;
end;
local procedure RegisterUpgradeTag()
var
UpgradeTag: Codeunit "Upgrade Tag";
UpgradeTagValue: Code[250];
begin
UpgradeTagValue := 'LBT-0001-ProcessingEntryEventNote-20260930';
if not UpgradeTag.HasUpgradeTag(UpgradeTagValue) then
UpgradeTag.SetUpgradeTag(UpgradeTagValue);
end;
}
codeunit 70507 "Entry Upgrade"
{
Subtype = Upgrade;
trigger OnCheckPreconditionsPerCompany()
var
Entry: Record "Processing Entry";
begin
if Entry.Count() > 100000 then
Error('Reduce the processing entry count before this upgrade.');
end;
trigger OnUpgradePerCompany()
begin
UpgradeEventNotes();
end;
trigger OnValidateUpgradePerCompany()
var
UpgradeTag: Codeunit "Upgrade Tag";
begin
if not UpgradeTag.HasUpgradeTag(GetUpgradeTag()) then
Error('The processing entry upgrade didn't complete.');
end;
local procedure UpgradeEventNotes()
var
Entry: Record "Processing Entry";
UpgradeTag: Codeunit "Upgrade Tag";
begin
if UpgradeTag.HasUpgradeTag(GetUpgradeTag()) then
exit;
Entry.SetRange("Event Note", '');
if Entry.FindSet(true) then
repeat
Entry."Event Note" := 'Entry prepared by upgrade.';
Entry.Modify(true);
until Entry.Next() = 0;
UpgradeTag.SetUpgradeTag(GetUpgradeTag());
end;
local procedure GetUpgradeTag(): Code[250]
begin
exit('LBT-0001-ProcessingEntryEventNote-20260930');
end;
}
Create EntryTask.Codeunit.al. The scheduler supplies the current company, a current DateTime as the earliest start, the entry's actual RecordId, and a Duration through the overload that accepts a timeout. Both task codeunits use the matching table as TableNo.
codeunit 70508 "Entry Task Scheduler"
{
procedure Schedule(var Entry: Record "Processing Entry")
var
TaskId: Guid;
TaskTimeout: Duration;
begin
Entry.TestField("Entry No.");
Entry.Status := Entry.Status::Ready;
Clear(Entry.Result);
Clear(Entry."Event Note");
Clear(Entry."Processed At");
Entry.Modify(true);
Commit();
TaskTimeout := 5 * 60 * 1000;
TaskId := TaskScheduler.CreateTask(
Codeunit::"Entry Task Runner",
Codeunit::"Entry Task Failure",
true,
CompanyName,
CurrentDateTime,
Entry.RecordId,
TaskTimeout);
Entry."Task ID" := TaskId;
Entry.Modify(true);
end;
}
codeunit 70509 "Entry Task Runner"
{
TableNo = "Processing Entry";
trigger OnRun()
var
ProcessService: Codeunit "Entry Process Service";
begin
ProcessService.Process(Rec);
end;
}
codeunit 70510 "Entry Task Failure"
{
TableNo = "Processing Entry";
trigger OnRun()
begin
Rec.Status := Rec.Status::Failed;
Rec.Result := 'Scheduled processing failed.';
Rec."Processed At" := CurrentDateTime;
Rec.Modify(true);
end;
}
Publish, validate, and clean up
- Add the source files to the existing sandbox-connected AL project, download symbols, compile, and publish the extension.
- Open Processing Entries. Use the installed sample entry or add an entry, enter a description, select Standard or Priority, and leave Force Failure cleared.
- Select Schedule processing, and then confirm that the task appears on the Scheduled Tasks page.
NotBeforeis only the earliest start, so refresh the entry without assuming a fixed completion time. - Confirm that a successful entry shows Completed, a mode-specific Result, and an Event Note from the subscriber. A successful one-time task no longer appears in Scheduled Tasks.
- Set Force Failure, schedule the entry again, and refresh both pages. Confirm that the main changes roll back and the failure codeunit records Failed and
Scheduled processing failed. - If a task remains queued or fails without the expected entry update, inspect Session Events and available Business Central telemetry for the background session, permissions, retry, timeout, or error details.
- Delete exercise entries and remaining scheduled tasks, and unpublish the extension when you finish using it.