Want to show different Dashbaord as homepage From 9am to 5pm and different from 5pm to 9am

vinay167
Tera Contributor

Want to show different Dashbaord as homepage From 9am to 5pm and different from 5pm to 9am how can we achieve that with the help of sys_id.
I have already used fix script to set the value of dashboard homepage in user_preference table but when i am opening a new dashboard it will automatically set as my homepage.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@vinay167 

may be you can use UI script and check the current time for that logged in user and then set the preference

I haven't tested this though.

1) create UI script and use GlideAjax to set the homepage

AnkurBawiskar_0-1749201304274.png

 

2) sample script include

var userId = gs.getUserID();
    var now = new GlideDateTime();
    var hour = parseInt(now.getDisplayValue().split(' ')[1].split(':')[0], 10);
    var ampm = now.getDisplayValue().split(' ')[2];
    if (ampm === 'PM' && hour !== 12) hour += 12;
    if (ampm === 'AM' && hour === 12) hour = 0;

    var dashboardUrl;
    // 9am to 5pm
    if (hour >= 9 && hour < 17) {
        dashboardUrl = '$pa_dashboard.do?embedded=true&sysparm_dashboard=SYS_ID_DAY'; // Replace SYS_ID_DAY
    } else {
        dashboardUrl = '$pa_dashboard.do?embedded=true&sysparm_dashboard=SYS_ID_NIGHT'; // Replace SYS_ID_NIGHT
    }

    // Set user preference for homepage
    var pref = new GlideRecord('sys_user_preference');
    pref.addQuery('user', userId);
    pref.addQuery('name', 'my_home_navigation_page');
    pref.query();
    if (pref.next()) {
        pref.value = dashboardUrl;
        pref.update();
    } else {
        pref.initialize();
        pref.user = userId;
        pref.name = 'my_home_navigation_page';
        pref.type = 'string';
        pref.value = dashboardUrl;
        pref.insert();
    }

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@vinay167 

may be you can use UI script and check the current time for that logged in user and then set the preference

I haven't tested this though.

1) create UI script and use GlideAjax to set the homepage

AnkurBawiskar_0-1749201304274.png

 

2) sample script include

var userId = gs.getUserID();
    var now = new GlideDateTime();
    var hour = parseInt(now.getDisplayValue().split(' ')[1].split(':')[0], 10);
    var ampm = now.getDisplayValue().split(' ')[2];
    if (ampm === 'PM' && hour !== 12) hour += 12;
    if (ampm === 'AM' && hour === 12) hour = 0;

    var dashboardUrl;
    // 9am to 5pm
    if (hour >= 9 && hour < 17) {
        dashboardUrl = '$pa_dashboard.do?embedded=true&sysparm_dashboard=SYS_ID_DAY'; // Replace SYS_ID_DAY
    } else {
        dashboardUrl = '$pa_dashboard.do?embedded=true&sysparm_dashboard=SYS_ID_NIGHT'; // Replace SYS_ID_NIGHT
    }

    // Set user preference for homepage
    var pref = new GlideRecord('sys_user_preference');
    pref.addQuery('user', userId);
    pref.addQuery('name', 'my_home_navigation_page');
    pref.query();
    if (pref.next()) {
        pref.value = dashboardUrl;
        pref.update();
    } else {
        pref.initialize();
        pref.user = userId;
        pref.name = 'my_home_navigation_page';
        pref.type = 'string';
        pref.value = dashboardUrl;
        pref.insert();
    }

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello Ankur,

yes it is working fine "Thank You for that" but do we have any method for cache clearing. Because we have to close current session and open new one (after 2-3 min) or need to clear cache. Can we add Something that in active session we can achieve above requirement.

@vinay167 

Glad to know that it's working.

Please mark my response as correct and close the thread.

The discussion can still continue on answered thread.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@vinay167 

try this in UI script


  // Only run on the homepage/dashboard
  if (window.location.pathname.indexOf('pa_dashboard.do') !== -1) {
    var now = new Date();
    var hour = now.getHours();
    var dashboardSysId;

    if (hour >= 9 && hour < 17) {
      dashboardSysId = 'YOUR_DAY_DASHBOARD_SYS_ID';
    } else {
      dashboardSysId = 'YOUR_NIGHT_DASHBOARD_SYS_ID';
    }

    // If not already on the correct dashboard, redirect
    if (window.location.search.indexOf(dashboardSysId) === -1) {
      window.location.href = '/$pa_dashboard.do?sysparm_dashboard=' + dashboardSysId;
    }
  }

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader