Service Portal: Dynamically load data in the widget.

Sreekarr Yallap
ServiceNow Employee
ServiceNow Employee

Hello Community,

In my Widget Server script, when user clicks on "onDemand Preflight check" button  I trigger a workflow that typically takes around 6 minutes to complete. 

Q: How can I dynamically load new content when the status of the workflow changes to "completed"? 

 

Screenshot 2025-02-14 at 2.23.36 PM.png

Q: Is using recordWatch() the correct approach to implement this functionality, or are there any better alternatives? If I apply recordWatch() on a table with a filter condition (such as looking for a specific state), it will monitor all records in that table. How can I target a specific record, for example, by sys_id?


6 REPLIES 6

Alternatively, I tried below code.. I don't think scope is getting updated. any suggestions how to load data dynamically?

 

   // Initialize a counter variable
    var refreshCount = 0;
    var maxRefreshes = 3; // We want the widget to refresh 3 times

    // Function to refresh the widget
    function refreshWidget() {
        if (refreshCount < maxRefreshes) {
            // spUtil.refresh() will refresh the widget content
                        //spUtil.refresh();
			//spUtil.update($scope);
			c.server.update();
			
            // Increment the counter
            refreshCount++;
            console.log('Widget refreshed ' + refreshCount + ' times.');

            // After 3 refreshes, stop the interval
            if (refreshCount === maxRefreshes) {
                console.log('Maximum refreshes reached. Stopping the refresh cycle.');
                $interval.cancel(refreshInterval); // Stop the interval after 3 times
            }
        }
    }

    // Set the interval to call the refreshWidget function every 1 minute
    var refreshInterval = $interval(function() {
        refreshWidget();
    }, 60000);

 

 

 

Record watch isn't supported on wf_context. It's blacklisted by default and enabling it can cause performance issues.