In this article, you will learn what is a Page Background Task and how to use it in Microsoft Dynamics 365 Business Central. With a simple example, you will be able to use a background task in a Role Center cue.

  • What Is A Page Background Task In Business Central?
  • The Parent-Child Relationship
  • How To Create A Simple Page Background Task
  • Bonus

What Is A Page Background Task In Business Central?


A page background task is a method for improving performance in Business Central. We can use a page background task to run heavy reading processes without blocking the user interface. By using this method users can continue working while the operations are executing in a second layer. This is a way to create faster and more responsive pages in Business Central where a better UI experience can be delivered.

The Parent-Child Relationship


When users are working in Business Central opening pages, performing tasks, etc, they are using a parent session. The second layer that we can use for page background tasks is called a child session. A child session is where the operations execute asynchronously. When they are finished, the child session notifies the parent with the results. You can learn in detail about it in the Microsoft documentation.

https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-page-background-tasks

How To Create A Simple Page Background Task


To illustrate a page background task, we will follow an example that will display the number of blocked customers in our company. We will run the process in a background task and show the result in a cue in the Role Center.

A Role Center cue is a perfect example for creating background tasks. The fastest the Role Center can load the better experience for the users.

If you want to learn more about how to create cues check this article:

The Task To Run (Child)


First of all, we need a codeunit (called ‘BackgroundTask’ for this example) to run the task at hand. We will place the code in the OnRun trigger.

This process will calculate the number of blocked customers in the background and will place the result in a dictionary.

The dictionary is needed because it is the type that will travel back to the parent session.

With the ‘Page.SetBackgroundTaskResult‘ method the dictionary will be available for the parent.


The Page (Parent) That Calls The Background Task


So now, how do we invoke the process in the codeunit?

The following image shows the cue that will be visible in the Role Center. It is on this page where the process begins and ends.

Thanks to the ‘CurrPage.EnqueueBackgroundTask‘ we can select the codeunit with our calculations. This is how we tell the system to start a child session.


You can get more information about this method in the Microsoft documentation.

https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/page/page-enqueuebackgroundtask-method


Now, we need to retrieve the result of the child session. This is done with the ‘OnPageBackgroundTaskCompleted‘ trigger. The ‘Result’ dictionary will store the data calculated in the codeunit.

We just need to retrieve the value and assign it to the ‘BlockedCustomers’ variable. We use Evaluate in this example to convert it from Text to Integer. As an Integer type is needed to show in the cue.


Handle Background Task Errors


We can handle any error that may occur during the child session thanks to the ‘OnPageBackgroundTaskError’ trigger. Here we can send a notification, keep an error log, or whatever we may need.


Final Touch


Finally, we can set the ‘OnDrillDown’ trigger to interact with the Role Center cue.


And, of course, don´t forget to extend the Role Center with the cue just created.


Now, let´s test it.


Bonus


We have tested that the process is doing it correctly. But, how do we know that the task is running in the background?

We can test it in our example by placing a Sleep like the following.


As you can see in this video, the number of blocked customers isn´t calculated yet. However, the user is already able to work with the Role Center without any problem. No blocking in the User Interface.



That´s all. Hope you find it useful.

How To Run Background Processes In Business Central: Page Background Tasks

Post navigation


Leave a Reply

Your email address will not be published. Required fields are marked *