A page background task is a child session that runs processes from a codeunit in the background, letting the user continue working on the Parent page session while the task runs.
If you are new to PAGE Backgroung Task for basic understanding you can refer Page Background Tasks in Microsoft Dynamics 365 Business Central 2019 release wave 2
Let understand this by taking an AL Code example.
Customer has a requirement to display the Count of Posted Sales Invoice for a Customer on Customer card.
This can be done in 2 steps.
- Create Page Background Task on Page:
- Create a background task codeunit.
Create Page Background Task on Page
We will first create Page Extension for Customer Card.
We will add a Text Var to display “Posted Sales Invoice Count” on Customer card and add the same in General Tab .
When a page background task is enqueued, parameters are passed as a dictionary of text to the codeunit’s OnRun trigger when the page background task session is started.
OnPageBackgroundTaskCompleted trigger of the page in the parent session is called, and the results of the task are passed to the trigger. The results are passed as a dictionary of text.
When an error occurs in the page background task codeunit, this trigger of the page in the parent session is automatically called and we need to handle the error.
Create a background task codeunit.
You need to create a new codeunit which will do all the calculation and run it in background. After the calculation have been performed the it needs to be passed back to the calling page for handling through SetBackgroundTaskResult and get data from Parent session through GetBackgroundParameters .
This was complete overview of the process. You can try this and improve the performace of the pages.
You can download the code from GIT.
For more details you can read Best Practices for AL Coding in Microsoft Dynamics Business Central (MSDYN365BC)
To get new updates and learn about Microsoft Dynamics 365 Business Central Microsoft Learn
Thanks for reading.