Servicenow Cheat Sheet



Sheet

No matter what system you’re working in, it is always critical to be able to identify information about the user who is accessing that system. Being able to identify who the user is, what their groups and/or roles are, and what other attributes their user record has are all important pieces of information that allow you to provide that user with a good experience (without giving them information they don’t need to have or shouldn’t have). ServiceNow gives administrators some pretty simple ways to identify this information in the form of a couple of user objects and corresponding methods. This article describes the functions and methods you can use to get information about the users accessing your system.

GlideSystem User Object

Idoubt if there’s a single concept in Service-now that is more valuable to understand than how to use GlideRecord methods to query, insert, update, and delete records in your system. These methods have a wide variety of uses and are found at the heart of many of the business rules, UI actions, and scheduled job scripts. ServiceNow; ServiceNow User Object Cheat Sheet; July 27, 2020. GlideSystem User Object. The GlideSystem (gs) user object is designed to be used in any server-side. ServiceNow Cheat Sheet Published: 23 May 2014 ID: G00339915 Analyst(s): Infrastructure and Operations Research Team Summary Many Infrastructure organizations are adopting or planning to adopt ServiceNow for IT Service Management. ServiceNow uses OKTA as a trusted partner providing authentication for our users. In order to access content or register for events through ServiceNow.com and ServiceNow properties, third-party cookies must be enabled on your browser. Click on your browser to learn how to enable cookies.

The GlideSystem (gs) user object is designed to be used in any server-side JavaScript (Business rules, UI Actions, System security, etc.). The following table shows how to use this object and its corresponding functions and methods.

Function/MethodReturn ValueUsage
gs.getUser()Returns a reference to the user object for the currently logged-in user.var userObject = gs.getUser();
gs.getUserByID()Returns a reference to the user object for the user ID (or sys_id) provided.var userObject = gs.getUser().getUserByID(’employee’);
gs.getUserName()Returns the User ID (user_name) for the currently logged-in user.
e.g. ’employee’
var user_name = gs.getUserName();
gs.getUserDisplayName()Returns the display value for the currently logged-in user.
e.g. ‘Joe Employee’
var userDisplay = gs.getUserDisplayName();
gs.getUserID()Returns the sys_id string value for the currently logged-in user.var userID = gs.getUserID();
getFirstName()Returns the first name of the currently logged-in user.var firstName = gs.getUser().getFirstName();
getLastName()Returns the last name of the currently logged-in user.var lastName = gs.getUser().getLastName();
getEmail()Returns the email address of the currently logged-in user.var email = gs.getUser().getEmail();
getDepartmentID()Returns the department sys_id of the currently logged-in user.var deptID = gs.getUser().getDepartmentID();
getCompanyID()Returns the company sys_id of the currently logged-in user.var companyID = gs.getUser().getCompanyID();
getCompanyRecord()Returns the company GlideRecord of the currently logged-in user.var company = gs.getUser().getCompanyRecord();
getLanguage()Returns the language of the currently logged-in user.var language = gs.getUser().getLanguage();
getLocation()Returns the location of the currently logged-in user.var location = gs.getUser().getLocation();
getDomainID()Returns the domain sys_id of the currently logged-in user (only used for instances using domain separation).var domainID = gs.getUser().getDomainID();
getDomainDisplayValue()Returns the domain display value of the currently logged-in user (only used for instances using domain separation).var domainName = gs.getUser().getDomainDisplayValue();
getManagerID()Returns the manager sys_id of the currently logged-in user.var managerID = gs.getUser().getManagerID();
getMyGroups()Returns a list of all groups that the currently logged-in user is a member of.var groups = gs.getUser().getMyGroups();
isMemberOf()Returns true if the user is a member of the given group, false otherwise.Takes either a group sys_id or a group name as an argument.
if(gs.getUser().isMemberOf(current.assignment_group)){
//Do something…
}
var isMember = gs.getUser().isMemberOf(‘Hardware’);
To do this for a user that isn’t the currently logged-in user…
var user = ‘admin’;
var group = “Hardware”;
if (gs.getUser().getUserByID(user).isMemberOf(group)){
gs.log( gr.user_name + ” is a member of ” + group);
}
else{
gs.log( gr.user_name + ” is NOT a member of ” + group);
}
gs.hasRole()Returns true if the user has the given role, false otherwise.if(gs.hasRole(‘itil’)){ //Do something… }
gs.hasRole()Returns true if the user has one of the given roles, false otherwise.if(gs.hasRole(‘itil,admin’)){
//If user has ‘itil’ OR ‘admin’ role then Do something…
}
hasRoles()Returns true if the user has any roles at all, false if the user has no role (i.e. an ess user).if(!gs.getUser().hasRoles()){
//User is an ess user…
}

It is also very simple to get user information even if the attribute you want to retrieve is not listed above by using a ‘gs.getUser().getRecord()’ call as shown here…//This script gets the user’s title
gs.getUser().getRecord().getValue(‘title’);


g_user User Object

The g_user object can be used only in UI policies and Client scripts. Contrary to its naming, it is not truly a user object. g_user is actually just a handful of cached user properties that are accessible to client-side JavaScript. This eliminates the need for most GlideRecord queries from the client to get user information (which can incur a fairly significant performance hit if not used judiciously).

g_user Property or MethodReturn value
g_user.userNameUser name of the current user e.g. employee
g_user.firstNameFirst name of the current user e.g. Joe
g_user.lastNameLast name of the current user e.g. Employee
g_user.userIDsys_id of the current user e.g. 681ccaf9c0a8016400b98a06818d57c7
g_user.hasRole()True if the current user has the role specified, false otherwise. ALWAYS returns true if the user has the ‘admin’ role.
Usage: g_user.hasRole(‘itil’)
g_user.hasRoleExactly()True if the current user has the exact role specified, false otherwise, regardless of ‘admin’ role.
Usage: g_user.hasRoleExactly(‘itil’)
g_user.hasRoles()True if the current user has at least one role specified, false otherwise.
Usage: g_user.hasRoles(‘itil’,’admin’)

It is often necessary to determine if a user is a member of a given group from the client as well. Although there is no convenience method for determining this from the client, you can get the information by performing a GlideRecord query. Here’s an example…//Check to see if assigned to is a member of selected group
var grpName = ‘YOURGROUPNAMEHERE’;
var usrID = g_form.userID; //Get current user ID
var grp = new GlideRecord(‘sys_user_grmember’);
grp.addQuery(‘group.name’, grpName);
grp.addQuery(‘user’, usrID);
grp.query(groupMemberCallback);
function groupMemberCallback(grp){
//If user is a member of selected group
if(grp.next()){
//Do something
alert(‘Is a member’);
}
else{
alert(‘Is not a member’);
}
}

To get any additional information about the currently logged-in user from a client-script or UI policy, you need to use a GlideRecord query. If at all possible, you should use a server-side technique described above since GlideRecord queries can have performance implications when initiated from a client script. For the situations where there’s no way around it, you could use a script similar to the one shown below to query from the client for more information about the currently logged in user.//This script gets the user’s title
var gr = new GlideRecord(‘sys_user’);
gr.get(g_user.userID);
var title = gr.title;
alert(title);


Useful Scripts

SheetServicenow Cheat Sheet

There are quite a few documented examples of some common uses of these script methods. These scripts can be found on the ServiceNow wiki.

Article from: https://www.servicenowguru.com/scripting/user-object-cheat-sheet/

Cheat
In my years as a ServiceNow Administrator, Developer, and Implementer there have been a few blogs that have repeatedly been a great source of information. The list below contains the blogs that I most frequently have found useful:
Servicenow query cheat sheet
  1. http://www.servicenowguru.com – (Mark Stanger – my Mentor at Crossfuze Solutions) – My favorite technical resource of ALL things ServiceNow, if you’ve been struggling or had a problem with it, there is probably a post about how to fix it here!
  2. http://www.john-james-andersen.com/category/blog/service-now – (John James Anderson) – good blog on Integrations (REST, etc.)
  3. http://snaug.com – (Sal Costa) – Good source of Jelly, CMS related info
  4. http://www.servicenowelite.com/blog – (Mike Kaufman) – Good source of everyday usage tips
  5. http://www.snc-blog.com – (Logicalis SMC) – Good source of everyday usage tips
  6. http://serviceportal.io – (Nathan Firth) – Good source of ServicePortal, CMS, Angular tips
  7. http://www.cavucode.com/blog?tag=GenevaGems – (John Roberts) – Good info on Geneva (GenevaGems) and Fuji (FujiForty) enhancements
  8. http://sncommander.com – (Joel Maxwell) – Good general source of ServiceNow tips
  9. http://www.goranlundqvist.com -(Goran Lundqvist) – Good solutions to specific use cases (now posts on SN blog too)
  10. http://servicenowtips.tumblr.com – Good ServiceNow nuggets, but hasn’t been updated in a while.
  11. https://community.servicenow.com – (ServiceNow) – Good Q&A on various issues
  12. https://community.servicenow.com/community/blogs/blog – (ServiceNow) Good posts about ideas/solutions for various issues
  13. https://hi.service-now.com/kb_view.do?sysparm_article=KB0552847 – (ServiceNow) Good CMS Resources – (Thank you to Robert Chrystie)
Good ServiceNow Related Books:

Mastering ServiceNow Administration – (Martin Wood) – Good book for Administrators at all levels. Takes the reader through creating a Hotel App which is a great way to cover all the various usages of the covered functionality.

Mastering ServiceNow Administration Second Edition – (Martin Wood) – Follow up to first version (above). Due out in October.

The Principles of Object-Oriented JavaScript – (Nicholas C. Zakas) – Great book for learning about how Objects work in JavaScript. From a ServiceNow perspective, think about how to apply these principles in your Script Includes.
As I’m sure there are some I’m missing, please respond in the comments to other great ServiceNow blogs/books you have found useful.

Servicenow Cheat Sheet Example

Related