How to set a role-based user preference

will_smith
Mega Guru

I have a "user preference" of navigator.collapsed that has no user, making it a system preference. But I only want to set this for users without the ITIL roe. This is very similar to the UI11 "glide.ui.navpage_state" that is set in our system, somehow it is role-based though. I'm not sure how the previous administrator set up this preference if the user does not have a certain role.

Here is the Name and Description for our current user preference that hides the navigator for all non-ITIL user...

Thank you everyone for taking time to read my post.

1 ACCEPTED SOLUTION

I don't think the script will run because it's not being called. Also, gs doesn't exist on client side scripts, it's only a server side code. Use g_user instead



Should be along the lines off:



addLoadEvent( function() {


if (!g_user.hasRole('itil')) {


  setPreference('navigator.collapsed', 'yes');


  }


});



However, I don't think that will do what you want because it won't refresh the site and so is happening too late. the applications would have already loaded expanded.



Also, from a performance perspective, I wouldn't advise doing it as a global ui script because each time you run setpreference SN will do a GlideRecord to insert the records and if/when the system gets busy, will make for a slower environment.



What I'd recommend is doing a script action.



Set it to event 'session.established'. There will be a checkbox called synchronous on that table which you check (it might need adding to the form and also have an acl against it which you'll need to set to admin overrides true).



Then on the script, just have:


if (!gs.hasRole('itil')) {


  gs.setPreference('navigator.collapsed', 'yes');


  }



That will set their preference on log in and then the page will be loaded



(slight disclaimer, I haven't checked if that preference is the right preference or not, but the code above is the concept for setting it on login - heres an example of setting another property that way Reverting The 'Modern Cell Coloring' Back to Pre-Eureka | Service Now Gems )


View solution in original post

10 REPLIES 10

ahmedhmeid1
Kilo Expert

Is there a script action that sets the user preference on login if they do not have the itil role?



Or a global UI script that does a setPreference? (wouldn't recommend this route)


Hi Ahmed, I've not worked with script actions yet, let me check.


If it's a script action, it will probably be triggered off the event session.established


will_smith
Mega Guru

I am not seeing anything in the event registry with the keyword role, I found a few for login - but they didn't seem to apply.