You may already be aware of this, but ServiceNow has the capability to set and retrieve user preferences within the platform. You can easily get or set these preferences using both server-side APIs as well as client-side APIs. This article will explain how you can do this both at the server level and at the client level.
API Functions
Server Side API Calls
Since user preferences are tied to and individual user, the server-side API gets you access to those user preferences at the gs user level. To access these APIs on the current user simply use the following method:
1 2 3 4 | //Set your current User Preference gs.getUser().setPreference("DESIRED_PREFERENCE_NAME", "DESIRED_PREFERENCE_VALUE"); //Get your user's current User Preference Value var prefVal = gs.getUser().getPreference("PREFERENCE_NAME"); |
To get the API on another user using a Server Side script, you would do the following:
1 2 3 4 5 6 | //Set a User Preference var user = gs.getUser().getUserByID("a2826bf03710200044e0bfc8bcbe5ded"); user.setPreference("DESIRED_PREFERENCE_NAME", "DESIRED_PREFERENCE_VALUE"); //Get the value for a specific User's User Preference var prefVal = user.getPreference("DESIRED_PREFERENCE_NAME"); |
Client Side API Calls
You can also allow your client scripts to access the current user’s user preferences in the following way:
1 2 3 4 5 | //Set your current user preference setPreference("DESIRED_PREFERENCE_NAME", "DESIRED_PREFERENCE_VALUE"); //Get your current user's preference getPreference("DESIRED_PREFERENCE_NAME"); |
Viewing the user preferences manually
You can also view and manage all user preferences as the system administrator. Simply navigate to User Administration > User Preferences.
Great information. I might look to this as a way of saving a particular catalog variable value that is specific to a user (approval routing code). This would allow a user to enter the value once the first time and have it populated on subsequent orders.
Thanks for the tips
Since Fuji release is around the block we cannot use javascript:gs.getUser().getPreference(“PREFERENCE_NAME”) in the list filters. got error “Illegal access to method getPreference(string) in class com.glide.sys.User”
So the way to use it is javascript:gs.getPreference(“PREFERENCE_NAME”)
Get ready for surprises.
Thanks,
Amit
Great point Amit…I hadn’t tried this on Fuji yet. I wonder if server side preference manipulation for other users will be extinct then since they are blocking the getUser() way of doing it. That being said, I guess you could always modify the table with GlideRecord, but not as simple a solution. Thanks for bringing this to my attention. I’ll note it in the blog post.
Great tutorial as always John! Thanks!
I didn’t knew that preference could be accessed client side as well … Thanks for sharing this 🙂