This post will explain how you can show the progress of a time consuming process in ServiceNow. We’ll achieve this via a Progress Worker.
A progress worker is a async task that runs on a separate thread so you can continue your other tasks while the task executes. An example is you may continue working while a plugin is being installed.
As you can see in the image below, the progress is shown with a live status of the plugin installation.The following components are required for the progress worker:GlideModal of “simple_progress_viewer” to show the progress
A Script Include that does the work and updates the progress
#Example
In this post I will create a progress viewer which shows the progress of creating incidents in bulk.
Let’s start of by creating the a UI Page which sets up the page with 2 input fields and a button:
Fields:
Name: create_incidents
In the above code you’ll notice the following:
Input field for the number of incidents to create
A button to trigger the creation
#2) UI Script to Create GlideModal
The aim of the JS UI Script is to initiate the GlideModal and call the Ajax Processor with the value of the two fields above. It will also create the required buttons on completion.
Create a UI Script with the following values:
Name: create_incident_progress
Global: false
In the above code you’ll notice the following:
A GlideModal is created referring to simple_progress_viewer
We set the ajax script with sysparm_ajax_processor and the parameters all start with sysparm_ajax_processor following their name
Specify the buttons to display on completion by, all buttons are prefixed with sysparm_button
An event listener is added on executionComplete which is called when the progress worker is completed. A tracker object is passed to the event listener which contains a map of values which you will see in the CreateIncidentWorker Script Include below.
Event listeners are attached to the buttons
#3) Create an Ajax Processor
We now need to create an ajax processor which will start the worker code when called.
Create a Script Include with the following values:
Name: CreateIncidentAJAXProcessor
Client callable: true
In the above code you’ll notice the following:
The start method is the entry point into the processor
A check is made to ensure a worker isn’t already running for the job
Create a GlideScriptedHierarchicalWorker with the CreateIncidentWorker Script Include that we’ll create below in step 4
#4) Create the Worker
Finally we need to create the worker Script Include which will do the nessacary work to complete the task. In this case it will create the specified number of incidents while updating the progress with a messageCreate a Script Include with the following values:
Name: CreateIncidentAJAXProcessor
Client callable: true
In the above code you’ll notice the following:
We get the last running tracker and set the appropriate details on the tracker, such as source table and maximum progress
Start the actual work by creating incidents and updating the progress on the tracker object
Update the final result on the tracker
Create a result object (aka tracker object) which will be passed to the client in step 2 in the executionComplete event handler.