# TaskScheduler.CreateTask(Integer, Integer [, Boolean] [, Text] [, DateTime] [, RecordId]) Method
> Version: _Available or changed with runtime version 1.0._
Adds a task to be run by the task scheduler. The task is ensured to not run before the specified time.
## Syntax
```al
[Task := ]  TaskScheduler.CreateTask(CodeunitId: Integer, FailureCodeunitId: Integer [, IsReady: Boolean] [, Company: Text] [, NotBefore: DateTime] [, RecordID: RecordId])
```
## Parameters
CodeunitId
Type: ../integer/integer-data-type
Specifies the ID of the codeunit to run.
FailureCodeunitId
Type: ../integer/integer-data-type
Specifies the ID of the codeunit to run if the task fails. If you do not want to provide a failure codeunit, then use 0.
*[Optional] IsReady*
Type: ../boolean/boolean-data-type
Sets the task to the ready state. A task cannot run unless it is ready.
*[Optional] Company*
Type: ../text/text-data-type
Specifies the company to run the task for. If you do not specify a company, the task will run in the user's current company.
*[Optional] NotBefore*
Type: ../datetime/datetime-data-type
Specifies the date and time that you want to run the task. When the task actually runs will depend on whether other tasks are running at the same time. The task will run the first opportunity on or after the date and time that you specify.
*[Optional] RecordID*
Type: ../recordid/recordid-data-type
Specifies the recordID of the record that you want to run the task on.
## Return Value
*[Optional] Task*
Type: ../guid/guid-data-type
## Remarks
Scheduled tasks are shown in the Scheduled Tasks page in the client. The TaskScheduler.CreateTask method creates a task and if the operation is successful, the task appears on the page (the implementation is different for the on-premises version compared to the online version of Business Central).
If one or both of the codeunits provided to TaskScheduler.CreateTask don't exist, then the method will throw an error at runtime.
> NOTE
> The first two parameters in TaskScheduler.CreateTask are integers, which represent the object IDs of codeunits. To code robustly, never supply the object ID directly in your code as numbers. Instead, use the :: operator as illustrated in the example above. With this technique, if any of the codeunits don't exist, you'll get a compile time error instead of a runtime error.
The TaskScheduler.CreateTask method also has a version that allows you to set a timeout parameter. See CreateTask(Integer, Integer, Boolean, Text, DateTime, RecordId, Duration)<taskscheduler-createtask-integer-integer-boolean-string-datetime-recordid-duration-method>
For more information about the task scheduler, see Using the Task Scheduler<../../devenv-task-scheduler>.
## Example (create a task to run now)
The following example schedules a task to run the MyCodeunit codeunit right now (when resources are available) in the current company and use the codeunit MyErrorhandlerCodeunit as the failure codeunit.
```al
begin
    TaskScheduler.CreateTask(CodeUnit::MyCodeunit, CodeUnit::MyErrorhandlerCodeunit);
end;
```
## Example (create a task to run after a specified point in time)
The following example schedules a task to run the MyCodeunit codeunit after a specified point in time (60 seconds + a random part of up to three seconds) and when resources are available. It runs in the current company and use the codeunit MyErrorhandlerCodeunit as the failure codeunit.
```al
begin
    TaskScheduler.CreateTask(
        CodeUnit::MyCodeunit,
        CodeUnit::MyErrorhandlerCodeunit,
        true,
        CompanyName,
        CurrentDateTime + 60*1000 + Random(3000)
    );
end;
```
## Related information
CreateTask(Integer, Integer, Boolean, Text, DateTime, RecordId, Duration)<taskscheduler-createtask-integer-integer-boolean-string-datetime-recordid-duration-method>
taskscheduler-data-type
Using the Task Scheduler<../../devenv-task-scheduler>
Get Started with AL<../../devenv-get-started>
Developing Extensions<../../devenv-dev-overview>

Copyright (c) Microsoft Corporation. CC BY 4.0, https://creativecommons.org/licenses/by/4.0/; code samples MIT. From https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/taskscheduler/taskscheduler-createtask-integer-integer-boolean-string-datetime-recordid-method, MicrosoftDocs/dynamics365smb-devitpro-pb at 828b6ee (2026-11-19). Changed: includes filled in, links to this site, and for agents shortened without dropping a fact. https://jonas-paprotka.de/docs/dev-itpro/developer/methods-auto/taskscheduler/taskscheduler-createtask-integer-integer-boolean-string-datetime-recordid-method/
