Task Scheduler is a new feature in NAV 2017 which delivers the following benefits:

  • Controlling when certain operations or processes are run;
  • Running multiple tasks at once to ensure if one of the sessions stops then all other tasks will continue to be executed;
  • Managing task action failovers easily if any tasks fails.

In this blog you will first find an overview of how Task Scheduler functionality works. Further, there is a quick example to showcase step-by-step actions in Task Scheduler. Finally, you will find out how already existing Job Queue functionality was improved with Task Scheduler.

How Does Task Scheduler Work in NAV 2017?

Let‘s get into the technical details. To begin, you should be aware that Task Scheduler is based on:

A new object type called TASKSCHEDULER having following 4 functions: CREATETASK, TASKEXISTS, CANCELTASK and SETASREADY.

A new system Table called Scheduled Tasks (ID 2000000175)

 When the TASKSCHEDULER.CREATETASK function is called – a new record is created in the “Scheduled Tasks” Table with values coming from CREATETASK function parameters. These tasks run in a background session between the Microsoft Dynamics NAV Server instance and database.

The session runs by using the same user/credentials that are used when calling C/AL code. When the time comes and the task is completed – it is deleted from the “Scheduled Tasks” Table.

Detailed information about parameters of CREATETASK function can be found in following MSDN page. Let me share an additional insight on the parameter – “IsReady”. If you would set it to TRUE this task would be executed right away (depending on value of “Not Before” field). However, you can leave it FALSE, meaning that this task would remain in “Scheduled Tasks” Table until the value would be changed to TRUE and this can be done using TASKSCHEDULER.SETTASKASREADY function.

Be aware that since the task(s) are running on background session, functions that require user interface (like CONFIRM) would cause the task(s) to fail. Besides the fact that the task(s) could fail if an usual error would be encountered in code, there are additional reasons:

  • The company cannot be opened.
  • An SQL connection or transient error occurred with the database.
  • The Microsoft Dynamics NAV Server instance restarted while the task was being run.
  • User does not have permissions to run objects that are used within task.

If an error occurs, unless the task(s) is interrupted by the failure codeunit, the Microsoft Dynamics NAV Server instance will rerun the task(s) according to the following flow/schedule:

  • Two minutes after the first failure.
  • Four minutes after the second failure.
  • Fifteen minutes after the third failure and subsequent failures.

As a result, you can view all:

  • Calls to attempt the task in “Session Event” table.
  • Errors in the event log of the computer that is running the Microsoft Dynamics NAV Server instance.

By default, Task Scheduler functionality is enabled, but it is possible to disable it in the Dynamics NAV 2017 Administration Tool. You can also set the maximum number of tasks that could run concurrent on the server.

Figure 1. Properties “Enable Task Scheduler” and “Maximum concurrent running tasks” in Dynamics NAV 2017 Administration Tool 

Quick Example of Task Scheduler in NAV 2017

I have created the following custom “DEMO” Table. This example will create a task that would enter a record to this “DEMO” Table.

Figure 2. New “Demo” Table

Then I have created “DEMO - Create Entry” Codeunit (ID 50000) that contains code, which would add a new record to my “DEMO” Table.

Figure 3. New “DEMO - Create Entry” Codeunit

I have also created another “DEMO - Create Error” Codeunit (ID 50005), which would execute in case the task would fail.

Figure 4. New “DEMO - Create ErrorCodeunit

The next step was to call the TASKSCHEDULER.CREATETASK function with following parameters:

Figure 5. TASKSCHEDULER.CREATETASK function

Once this codeunit is executed, a new record is created in “Scheduled Tasks” Table (in same Company, because Company = COMPANYNAME) with ID = {cbdc7bba-fb86-47ba-938e-0776221cb750}

Figure 6. Record created in “Scheduled Tasks” Table

After 15 seconds (because NotBefore = CURRENTDATETIME + 15000) codeunit ID 50000 (because CodeunitId  = CODEUNIT :: “DEMO - Create Entry”) will be executed, which would create a record in Demo table

Figure 7. Record created in “Demo” Table

Once the task is completed, the related record from “Scheduled Tasks” Table is deleted.

One more important thing to add is that in the  “Session Event” system table (ID 2000000111) you can find all active background sessions started for each running task.

Figure 8. Records created in “Session Event” Table

How Job Queue Functionality was Improved with Task Scheduler?

There are 2 new fields in the “Job Queue Entry” Table: “System Task ID” and “Scheduled”. Once the Status of Job Queue Entry is set to “Ready”, the TASKSCHEDULER.CREATETASK function is called in “Job Queue – Enqueue” Codeunit (ID 453), which would create record in “Scheduled Tasks” table and fill System Task ID, (i.e.“Job Queue Entry “.”System Task ID”  = “Scheduled Tasks”.ID).

Codeunit “Job Queue Dispatcher” (ID 448) would be handling the task -  executing the object that is specified in “Job Queue Entry” Card as “Object ID to Run” at needed time.

Once it is done – the old record is deleted from “Scheduled Tasks” Table and the new one is created with updated “Not Before” field value, based on recurrence rules in “Job Queue Entry”Failures would be handled in “Job Queue Error Handler” Codeunit (ID 450).

Figure 9. Records created in “Scheduled Tasks” Table regarding Job Queue entries 

Now You’re Ready to Task Schedule in NAV 2017!

Microsoft Dynamics NAV 2017’s Task Scheduler offers a variety benefits and possibilities for you to leverage today. I hope this blog has provided you with the information and examples you need to get started with Task Scheduler in your NAV 2017 system.

Happy Scheduling! :)