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

Kieran Anson
Kilo Patron

Hi,

the filter you pass into recordWatch can be a filter for a specific record. You'd use the sys_id to monitor a single record. With the process being 6 minutes, are your users going to wait that long? Or is it best to fire a notification for them to then return and view the result?

spUtil.recordWatch($scope,"incident","sys_id=<sys_id_goes_here>",function(name){

})

 

Hello @Kieran Anson I will definitely send a notification to prompt the user to return and view the result. Upon their return, if the workflow is still running, I want to load the results dynamically.

Could you help me understand how to invoke server-side code from a record watch? How can I retrieve the latest server-side results?

recordwatcher receives an update whenever the record is modified. I assume you're wanting to watch a record until a certain state occurs? You could wait for that record to be updated an then fire a function call within your record watcher script

 

Psuedo code example:

 

spUtil.recordWatch($scope,"incident","sys_id=<sys_id_goes_here>",function(name){

	if(event.data && event.data.changes.includes('state')){
		$scope.server.get({
			'action' : 'doSomething'
		}).then(function(response){
			//do something
		})
	}

})

@Kieran Anson In my widget, when the user clicks the "OnDemand Preflight" button, I trigger a backend workflow and pass the workflow's sys ID to my client controller (line 24). After receiving the sys ID, I use recordWatch to monitor the state until it finishes. Once the state is completed, I intend to load the latest data from the server into the widget.

However, the issue I'm encountering is that I'm unable to see any console.log statements inside the recordWatch.

Screenshot 2025-02-19 at 12.19.07 PM.png