Abhay Kumar1
Giga Sage

Overview :  Missing auto refresh functionality on dashboard , there are some scenario where customers  needed this functionality which is quite helpful to them. I have also came across same situation where asked to provide an option which allow the same. 

One of Solution : We can create a score type report and place it on Dashboard, then click on show real-time updates on setting of that widget, this also provide us real time count of records. But this was unfortunate for us that some of times this was also not refreshing count, i don't know what is reason but has been seen on prod instance.

Other workaround:  I found a comment of @gav1n on an article  with some code , i was so happy to see at least i have found something but that code did not work as expected but for sure was helpful to come across to get a final working code on dashboard.  I am sharing with you working code which will allow to get refresh all reports on a specific Tab of dashboard. If you wish to use on other tab you can add or on any dashboard as well. 

Create an dynamic content : Add Widgets -> Widget Category=content Blocks -> select *New Dynamic Content 

Once displayed on dashboard edit it and paste below code ,

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	<style>.counter{display:block;margin:2px 0px;color:#969696}.refresh-select{width:auto;display:inline}</style>
	<script>
		var r_sec = 0;
		var t_now = null;
		var t_next = null;
		var t_last = null;
		var myInterval = null;
		var r_active=true;
		var tab_active=false;
		//auto read seconds on selector change
		function readTime() {
			r_sec = gel('sel_time').value;
			localStorage['saved_time'] = r_sec;
			clearInterval(myInterval);
			reloadDashboard();
		}
		
		// refresh using actual page script
		function refreshPage() {
		var scope = angular.element(document.getElementsByClassName("icon-cards btn btn-icon btn-default sn-tooltip-basic")).scope();
		scope.$apply(function(){scope.msg = ' '; })
		scope.refreshAllPanes();	
		}
		
		//auto read active (true/false) on selector change
		function readActive() {
			localStorage['saved_active'] = r_active;
			clearInterval(myInterval);
			reloadDashboard();
		}
		function readTabChange() {
			r_tabs =tab_active; // gel('tab_active').value;
			localStorage['saved_tabs'] = r_tabs;
		}	
		//refresh now on button click
		function refreshNow() {
			refreshPage();
			clearInterval(myInterval); //always clear interval after refresh
		}
		//run interval every 1 second
		function runInterval(){
//console.log("abhaytest1");
			if(t_now &lt; t_next) {
				// the definition of "('0' + t_X).slice(-2)" is there to print the leading zeros in front
				$j('#refresh .counter').text('Now: '+t_now.getHours()+':'+ ('0'+t_now.getMinutes()).slice(-2) +':'+ ('0'+t_now.getSeconds()).slice(-2) +' Next: '+t_next.getHours()+':'+('0'+ t_next.getMinutes()).slice(-2) +':'+ ('0'+t_next.getSeconds()).slice(-2) +' Last: '+t_last.getHours()+':'+ ('0'+t_last.getMinutes()).slice(-2) +':'+ ('0'+t_last.getSeconds()).slice(-2));
		
				//console.log('Now: '+t_now.getSeconds()+', Next: '+t_next.getSeconds());
		
				t_now.setSeconds(t_now.getSeconds() + 1);
			}else {
			refreshPage();
				clearInterval(myInterval); //always clear interval after refresh
			}
		}
		function reloadDashboard() {
			//read time in seconds
		    r_sec = localStorage['saved_time'] || gel('sel_time').value; //read saved option from cache after refresh or from the selector
			$j('#sel_time').val(r_sec); //we set it to the selector
				t_now = new Date(Date.now());
				t_next = new Date(t_now.getTime());
				t_next.setSeconds(t_next.getSeconds() + parseInt(r_sec)); //we sum read seconds to calculate next refresh
				t_last = new Date(t_now.getTime()); //save last run, as t_now is displayed on real time. eg. t_now=t_now+1
				myInterval = setInterval(runInterval, 1000); //run every 1 sec.
		}
		
		$j('#sel_time').val('300'); //default option is 60 seconds
		reloadDashboard();

		//if window tab is active (for optimal performance)
		$j(window).focus(function() {
			clearInterval(myInterval);
			reloadDashboard();
		});
		$j(window).blur(function() {
			//if(r_tabs == 'false') {
			clearInterval(myInterval);
			$j('#refresh .counter').text('Stopped. Tab was inactive.');
		});
	</script>
	<div id='refresh' class='panel-body'>
			<span class='counter'></span>
		Refresh:
		<select id='sel_time' class='form-control refresh-select' onchange='readTime()'>
			<option value='30'>30 sec.</option>
			<option value='60' selected="selected">1 min.</option>
			<option value='300'>5 min.</option>
			<option value='900'>15 min.</option>
			<option value='1800'>30 min.</option>
		</select>
		<button type='button' class='btn btn-default' onclick='refreshNow()'>Refresh now</button>
	</div>
</j:jelly>

 

Reason for sharing is what someone shared helped me likewise i am sharing with same hope.

Thanks,

Ab

Comments
Void84
Giga Guru

Tried this with Tokyo update and it isn't working for me.  Is there any other settings when editing that must be done?

 

Edit: NM I figured it out. Thanks!

Abhay Kumar1
Giga Sage

@Void84 This is to just create a dynamic content and the same implemented in my organization and even i dont see any issue on Tokyo.

I can help you if you let me know what are you facing issue with screen shot?

Thanks

Yaalini Sakthiv
Giga Contributor

Hi Abhay,

It was so helpful. Can you please tell me how to set default time in the content block. I want always 2 mins as a default time in the refresh widget if the users want some other time they can select it with the dropdown which is already added. Can you please guide me in this?

 

Thanks in advance

Micky6002
Tera Expert

Hi Abhay, why I could see nothing after I created Dynamic Content? I'm unable to edit it at all. By the way I don't have an admin account.

Dynamai Content.png

AnttiP
Tera Contributor

Does this (custom autorefresh) keep logged in user's session on from now to eternity?

Abhay Kumar1
Giga Sage

@Micky6002 it seems you don't have access. 

Abhay Kumar1
Giga Sage

@AnttiP no it's won't, there are piece of code which checks if it's not blur.

Sam Motley
Giga Guru

@Abhay Kumar1 cheers for providing this really great script. 

One thing i'm trying to do is not stop the countdown if the tab is inactive as i would like this to refresh whether or not someone is viewing the tab 

Any thoughts? I've tried commenting out some of the script but the timer goes crazy and completely out of sync 

 

cheers

Abhay Kumar1
Giga Sage
  • @Sam Motley if you don't stop if user is not on dashboard then session will never get expired, so here i will not suggest to go for it.
ArndtAaron
Giga Explorer

Hi Abhay,

Thanks for the code and apologize if this is necro'ing a old post. Is there a way to restrict a single report on the dashboard from updating/refreshing?

The Incident
Tera Contributor

Incase anyone had the same problem we did.

 

We had a problem where the script went inactive when the tab in the browser wasnt selected or active. What i dis to fix this was remove this part of the script and it has worked for about a day now:

 

$j(window).blur(function() {
			//if(r_tabs == 'false') {
			clearInterval(myInterval);
			$j('#refresh .counter').text('Stopped. Tab was inactive.');
		});
jessy_guindi
Tera Explorer

Hi @Abhay Kumar1,

Thank you so much for sharing this great script!

Quick question, if i wanted to add an "Off" option for the user to select in case he did not want to have the automatic refresh and just wants to use the manual option ("Refersh now" button).

Can you please advise what should be done here in the script?

Thanks.

Abhay Kumar1
Giga Sage

@jessy_guindi This will not work in that way because this filter would be applicable for all users. So if you change anything in filter functionality same would be for other user.

 

 

adnaanC
Tera Explorer

Thank you it is working perfectly.

BirgitH
Tera Contributor

Auto refresh dashboard using dynamic content view - ServiceNow Community

Hi,

We observed a problem when the tab in the browser wasn't selected, the timer will go crazy. When you go back again to the tab(with the Dynamic auto-refresh is in place), the timer will run in a fast forward phase(x2 up to x10 faster than the normal).

Is there a solution to this topic?

Abhay Kumar1
Giga Sage

@BirgitH are you using same code shared in this article?

Because more than a year i implemented this in one of dashboard used per day by multiple users for P1 & P2 incidents, we never found this behaviour so far.

BirgitH
Tera Contributor

Hi Abhay Kumar1,

 

yes i have copied the script.

Only this part I have reduced from Comment: ‎01-23-2024 03:19 AM:

We use edge as a fixed browser.

On the little mp4 you can see it, how does the timer work i can not upload to you.

Following steps:

1. start dashboard the time runs normal in state "now".

2. after i go to another tab in dashboard and go back to the dyn. content block the time runs 2x - 10x faster.

Rajkumar Bokken
Tera Contributor

Hi @Abhay Kumar1 

 

We have implemented this auto refresh widget in our dashboard with  and saw the same issue that @BirgitH have explained. - We observed a problem when the tab in the browser wasn't selected, the timer will go crazy. When you go back again to the tab(with the Dynamic auto-refresh is in place), the timer will run in a fast forward phase(x2 up to x10 faster than the normal).

Is there a solution to this topic?

 

 

Thanks

Rajkumar

Abhay Kumar1
Giga Sage

@Rajkumar Bokken Are you amending anything in provided script , if so then will be required additional steps to be taken care, as I had same issue and amended code not to occur this scenario.

BirgitH
Tera Contributor

Hello Kumar1,

i have used the original skript also and then occures like message from (incident: tera contributor) ‎01-23-2024 03:19 AM.  So i have reduced the code like in this message and the timer runs too fast. What steps will be needed that the timer runs normal?

Tsuki
Giga Explorer

Hi @Abhay Kumar1 and to all,

Is there a resolution now to what posted above by BirgitH about the timer running fast and crazy when switching to another tab. Same exact code was used and we are experiencing the exact problem too.
Thank you in advance! 🙂

Tsuki
Giga Explorer

Hi @Abhay Kumar1  and all, 
Is there a resolution to the current issue posted by @BirgitH ? We are on the same page and using the exact same script, but we're encountering issues where the timer speeds up and behaves erratically when switching tabs. 
Thanks in advance 🙂

Version history
Last update:
‎08-05-2022 09:54 AM
Updated by: