Operate resilient scheduled tasks
The service and lifecycle code prepare extension-owned entries for processing. A scheduled task moves that operation into a separate background session, so its design must account for identity, transactions, and failure without a page context.
Create a ready task with context
TaskScheduler.CreateTask registers a one-time task. Supply the main codeunit ID, optional failure codeunit ID, ready state, company, earliest start time, and record payload. If you use the corresponding overload, also supply a duration timeout. The record payload is a RecordId, and a codeunit with a matching TableNo receives it as Rec.
TaskId := TaskScheduler.CreateTask(
Codeunit::"Entry Task Runner",
Codeunit::"Entry Task Failure",
true,
CompanyName,
CurrentDateTime,
Entry.RecordId,
TaskTimeout);
A ready task is eligible to run. NotBefore is the earliest start time, not a guaranteed execution time. Capacity, retries, maintenance, and other work affect when the platform starts it. The task runs in the specified company in a separate background session under the same user identity and permissions as the user who schedules it.
Background code must not call Message, Confirm, pages, reports, or other user-interface operations. Pass durable context through the record and persist results to tables.
Design transaction and failure behavior
The main task codeunit runs in a transaction. If it ends successfully, Business Central commits its changes and removes the task from the Scheduled Tasks table. If it fails, the main transaction rolls back.
The platform distinguishes failure classes. Failures that can be retried, such as transient database conditions, follow the task scheduler retry policy until the task succeeds or exhausts its retries. For a nonretriable failure, an optional failure codeunit runs in a new session and transaction. Because the main transaction rolled back, the failure codeunit can persist a visible failed status on the extension-owned record.
Both codeunits should use the same TableNo when they receive the same RecordId payload:
codeunit 70509 "Entry Task Runner"
{
TableNo = "Processing Entry";
trigger OnRun()
begin
ProcessEntry(Rec);
end;
}
codeunit 70510 "Entry Task Failure"
{
TableNo = "Processing Entry";
trigger OnRun()
begin
Rec.Status := Rec.Status::Failed;
Rec.Modify(true);
end;
}
Make processing repeatable because a retried or manually rescheduled operation can encounter data that a prior attempt partially affected outside the database transaction. Avoid overlapping work by using record state, locking, or another explicit concurrency strategy appropriate to the operation.
Monitor unattended execution
Use the Scheduled Tasks page to confirm that a task is ready, inspect its earliest start, or identify a task that remains queued. A successful one-time task disappears from the table, so verify the extension-owned result rather than treating disappearance alone as proof of business success.
Use Session Events and Business Central telemetry to investigate delayed starts, failed background sessions, duration, and errors. Capture enough task and record context in telemetry to connect an operational failure to the affected entry without exposing sensitive data.
A scheduled task represents one deferred operation managed by extension code. A job queue entry is a user-managed, recurring operational schedule with its own administration experience. Choose the job queue when users need recurrence and operational controls. Choose a scheduled task when the extension schedules a specific one-time unit of work.
The exercise combines these decisions in one small extension-owned processing flow.
Learn more in Task scheduler and TaskScheduler data type.