Default dashboard based on group assignment

richardhofing
Mega Expert

I am exploring the possibility of being able to specify a user's default dashboard based on the group they are assigned to. Is this possible?   This would help when a user changes to a new group at which point the relevant dashboard would be displayed.

Ric

3 REPLIES 3

Manjeet Singh
ServiceNow Employee
ServiceNow Employee

Hi Rick,



This can be done in the reporting dashboard



Please check section 4 and 5 here for more info.


Adding Existing Gauges to a Homepage - ServiceNow Wiki



Thanks,


Manjeet



PS: Hit like, Helpful or Correct depending on the impact of the response


Dennis R
Tera Guru

You could create a business rule on the sys_user_grmember table to run after an insert or update. In the script field (Advanced), set the user's homepage preference to be the homepage or dashboard you want based on the group.



As a simple example, in my personal instance, I created the following business rule:


homepage br.png


For the script, I entered the following:



/**


* Sets a user's home to either a homepage or dashboard per the business logic


* at the bottom of this function.


*


* Created by Dennis R


* 2017-08-22: Initial creation


*/


(function assignHomepage(current, previous) {


      /**


        * AUXILIARY FUNCTIONS


        * There may be an easier way to do this, like via an out-of-the-box script


        * include, I'm not sure. If there's not, you could put the following two


        * function in a script include yourself. As it is, though, I've just put


        * them as auxiliary functions right here in the business rule for the


        * sake of convenience.


        */


     


      /**


        * Given a user's sys_id and a PA dashboard name, set's the user's default


        * homepage to that PA dashboard.


        */


      var setPADashboard = function(userSysId, dashboardName) {


             


              // Fetch dashboard


              var grDashboard = new GlideRecord('pa_dashboards');


              if (!grDashboard.get('name', dashboardName))


                      return;




              // Make sure user's home page is set to dashboards


              var grPref = new GlideRecord('sys_user_preference');


              grPref.addQuery('user', userSysId);


              grPref.addQuery('name', 'my_home_navigation_page');


              grPref.query();


              if (grPref.next()) {


                      if (grPref.getValue('value') !== '$pa_dashboard.do') {


                              grPref.setValue('value', '$pa_dashboard.do');


                              grPref.update();


                      }


              }


              else {


                      grPref = new GlideRecord('sys_user_preference');


                      grPref.initialize();


                      grPref.setValue('user', userSysId);


                      grPref.setValue('name', 'my_home_navigation_page');


                      grPref.setValue('value', '$pa_dashboard.do');


                      grPref.insert();


              }


             


              // Set the user preference to point to that dashboard


              grPref = new GlideRecord('sys_user_preference');


              grPref.addQuery('user', userSysId);


              grPref.addQuery('name', 'com.snc.pa.ui.preferences_dashboards');


              grPref.query();


              if (grPref.next()) {


                      var dash_settings = JSON.parse(grPref.getValue('value'));


                      dash_settings.last = grDashboard.getValue('sys_id');


                      grPref.setValue('value', JSON.stringify(dash_settings));


                      grPref.update();


              }


              else {


                      grPref = new GlideRecord('sys_user_preference');


                      grPref.initialize();


                      grPref.setValue('user', userSysId);


                      grPref.setValue('name', 'com.snc.pa.ui.preferences_dashboards');


                      grPref.setValue('value', JSON.stringify({


                              last: grDashboard.getValue('sys_id')


                      }));


                      grPref.insert();


              }


      };


     


      /**


        * Given a user's sys_id and a homepage name, set's the user's default


        * homepage to that PA homepage.


        */


      var setHomepage = function(userSysId, homepageName) {


              // Fetch dashboard


              var grHomepage = new GlideRecord('sys_portal_page');


              if (!grHomepage.get('title', homepageName))


                      return;




              // Make sure user's home page is set to homepages


              var grPref = new GlideRecord('sys_user_preference');


              grPref.addQuery('user', userSysId);


              grPref.addQuery('name', 'my_home_navigation_page');


              if (grPref.next()) {


                      if (grPref.getValue('value') !== 'default') {


                              grPref.setValue('value', 'default');


                              grPref.update();


                      }


              }


              else {


                      grPref = new GlideRecord('sys_user_preference');


                      grPref.initialize();


                      grPref.setValue('user', userSysId);


                      grPref.setValue('name', 'my_home_navigation_page');


                      grPref.setValue('value', 'default');


                      grPref.insert();


              }


             


              // Set the user preference to point to that homepage


              grPref = new GlideRecord('sys_user_preference');


              grPref.addQuery('user', userSysId);


              grPref.addQuery('name', 'homepage');


              grPref.query();


              if (grPref.next()) {


                      grPref.setValue('value', grHomepage.getValue('sys_id'));


                      grPref.update();


}


              else {


                      grPref = new GlideRecord('sys_user_preference');


                      grPref.initialize();


                      grPref.setValue('user', userSysId);


                      grPref.setValue('name', 'homepage');


                      grPref.setValue('value', grHomepage.getValue('sys_id'));


                      grPref.insert();


              }


      };


     


      /**


        * BUSINESS LOGIC


        * This is where you determine what groups should have what page as their


        * default homepage or dashboard.


        *


        * Note that this might irritate some users who have changed their default


        * from what it started out as, because whenver you assign them to a new


        * group that is included below, it will override that default and make


        * them change it again back to what it was, if they want to do that.


        */


     


      // Check to see if the user is being added to the Network or Service Desk


      // groups, and if so, set their default home page to the Incident


      // Management PA dashboard


      if (current.getDisplayValue('group') === 'Network' ||


                      current.getDisplayValue('group') === 'Service Desk') {


              setPADashboard(current.getValue('user'), 'Incident Management');


      }


     


      // Check to see if the user is being added to the Hardware group, and if


      // so, set their default page to the ITIL Homepage


      else if (current.getDisplayValue('group') === 'Hardware') {


              setHomepage(current.getValue('user'), 'ITIL Homepage');


      }


     


      // If the user is in a group not specified above


      else {


              // Don't edit their homepage. Or do. The choice is yours!


      }


})(current, previous);



In my example script above, I set the default homepage to the ITIL Homepage for any users added to the Hardware group, and the default homepage to the Incident Management PA dashboard for any users added to the Network or to the Service Desk group.   Of course, you'd have to customize the script to meet your needs, designating which groups would get set to what homepages or PA dashboards (lines 136-139 and 143-145 above), but this should get you almost all the way to what you asked.



Edit: Somehow, a couple of lines (68-69) got duplicated when I pasted the script in originally. I've corrected it above.



Hope this helps,


--Dennis R


Arnoud Kooi
ServiceNow Employee
ServiceNow Employee

Nice one Dennis!


I'll try it in the next few days. Maybe you could even call it after each login!