{"title":"TaskScheduler.CreateTask(Integer, Integer [, Boolean] [, Text] [, DateTime] [, RecordId]) Method","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/methods-auto/taskscheduler/taskscheduler-createtask-integer-integer-boolean-string-datetime-recordid-method/","by":"JOP-AL","license":"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.","credit":"Microsoft Corporation, CC BY 4.0","origin":"https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/taskscheduler/taskscheduler-createtask-integer-integer-boolean-string-datetime-recordid-method","source":{"name":"Business Central developer and IT pro docs","repo":"https://github.com/MicrosoftDocs/dynamics365smb-devitpro-pb","commit":"828b6ee72fcd75755627cd6011f3d20899217ab2","date":"2026-11-19"},"checked":"2026-11-19","description":"Adds a task to be run by the task scheduler.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":1,"title":"TaskScheduler.CreateTask(Integer, Integer [, Boolean] [, Text] [, DateTime] [, RecordId]) Method","slug":"taskschedulercreatetaskinteger-integer-boolean-text-datetime-recordid-method","at":0,"chars":3999},{"level":2,"title":"Syntax","slug":"syntax","at":262,"chars":192},{"level":2,"title":"Parameters","slug":"parameters","at":454,"chars":1008},{"level":2,"title":"Return Value","slug":"return-value","at":1462,"chars":63},{"level":2,"title":"Remarks","slug":"remarks","at":1525,"chars":1198},{"level":2,"title":"Example (create a task to run now)","slug":"example-create-a-task-to-run-now","at":2723,"chars":348},{"level":2,"title":"Example (create a task to run after a specified point in time)","slug":"example-create-a-task-to-run-after-a-specified-point-in-time","at":3071,"chars":568},{"level":2,"title":"Related information","slug":"related-information","at":3639,"chars":360}],"text":"# TaskScheduler.CreateTask(Integer, Integer [, Boolean] [, Text] [, DateTime] [, RecordId]) Method\n> Version: _Available or changed with runtime version 1.0._\nAdds a task to be run by the task scheduler. The task is ensured to not run before the specified time.\n## Syntax\n```al\n[Task := ]  TaskScheduler.CreateTask(CodeunitId: Integer, FailureCodeunitId: Integer [, IsReady: Boolean] [, Company: Text] [, NotBefore: DateTime] [, RecordID: RecordId])\n```\n## Parameters\nCodeunitId\nType: ../integer/integer-data-type\nSpecifies the ID of the codeunit to run.\nFailureCodeunitId\nType: ../integer/integer-data-type\nSpecifies the ID of the codeunit to run if the task fails. If you do not want to provide a failure codeunit, then use 0.\n*[Optional] IsReady*\nType: ../boolean/boolean-data-type\nSets the task to the ready state. A task cannot run unless it is ready.\n*[Optional] Company*\nType: ../text/text-data-type\nSpecifies the company to run the task for. If you do not specify a company, the task will run in the user's current company.\n*[Optional] NotBefore*\nType: ../datetime/datetime-data-type\nSpecifies 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.\n*[Optional] RecordID*\nType: ../recordid/recordid-data-type\nSpecifies the recordID of the record that you want to run the task on.\n## Return Value\n*[Optional] Task*\nType: ../guid/guid-data-type\n## Remarks\nScheduled 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).\nIf one or both of the codeunits provided to TaskScheduler.CreateTask don't exist, then the method will throw an error at runtime.\n> NOTE\n> 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.\nThe 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>\nFor more information about the task scheduler, see Using the Task Scheduler<../../devenv-task-scheduler>.\n## Example (create a task to run now)\nThe 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.\n```al\nbegin\n    TaskScheduler.CreateTask(CodeUnit::MyCodeunit, CodeUnit::MyErrorhandlerCodeunit);\nend;\n```\n## Example (create a task to run after a specified point in time)\nThe 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.\n```al\nbegin\n    TaskScheduler.CreateTask(\n        CodeUnit::MyCodeunit,\n        CodeUnit::MyErrorhandlerCodeunit,\n        true,\n        CompanyName,\n        CurrentDateTime + 60*1000 + Random(3000)\n    );\nend;\n```\n## Related information\nCreateTask(Integer, Integer, Boolean, Text, DateTime, RecordId, Duration)<taskscheduler-createtask-integer-integer-boolean-string-datetime-recordid-duration-method>\ntaskscheduler-data-type\nUsing the Task Scheduler<../../devenv-task-scheduler>\nGet Started with AL<../../devenv-get-started>\nDeveloping Extensions<../../devenv-dev-overview>"}