Using Task Scheduler in Business Central
Introduction:
In Business Central, we can use Task Scheduler to utilize background processing to ensure that Users do not have to wait for completion of long operations. Task Scheduler provides us an easy to use and monitor way to offload tasks from the main thread. It creates separate sessions for processing and it allows creating multiple sessions which can be used to run tasks in parallel. It also allows us to define the company in which the processing will happening which is useful when we have to perform operations across all available companies, for example initialization of certain fields or creation of certain records on extension installation.
Pre-requisites:
Business Central OnCloud or OnPremise
References:
TaskScheduler Data Type – Business Central | Microsoft Docs
Task Scheduler – Business Central | Microsoft Docs
Configuration:
The following procedures are available for the Task Scheduler data type:
- CanCreateTask() – Checks whether it is possible to create a new task in this session.
- CreateTask() – Creates a new task with the specified codeunit, we also specify which Codeunit to run if the main codeunit fails, whether the starts of this task would be Ready when it is created, the company this task will be running under and the time after which this task is to be run.
- TaskExists() – Checks whether the specified Task exists or not.
- SetTaskReady() – Sets the specified task status to Ready, a Task can only begin processing if it is in Ready state.
- CancelTask() – Sets the specified Task’s status to Cancel. A Task can only be set to cancel if it is in pending state. A task that is in progress cannot be cancelled.
In case the main Codeunit hits an exception then there are two cases:
- The exception is retriable :- Business Central will re-try the main codeunit a specific number of times with a specific time interval, if it is unable to complete then the task is failed.
- The exception is not retriable :- The task fails and the session is deleted.
Further if there is a failure codeunit defined then Business Central will run the Failure codeunit instead of failing the task and similarly if the Failure Codeunit hits an exception that is it unable to handle then there are same two cases:
- The exception is retriable :- Business Central will re-try the main codeunit a specific number of times with a specific time interval, if it is unable to complete then the task is failed.
- The exception is not retriable :- The task fails and the session is deleted.
I have created an action which creates a simple task for all the companies available to the User. After that action is called, we can see that there are four separate sessions created with the same codeunit for different companies.
The below is the screenshot of the page – Scheduled Tasks
Conclusion:
Thus we saw how to create and use Scheduled Tasks in Business Central for background processing.
Business Central OnPremise requires some configuration before Task Scheduler can be used you can refer to the below link for the same.
Microsoft Dynamics 365 Business Central Server Configuration – Business Central | Microsoft Docs