GlideSystem

GlideSystem is probably the most commonly used server-side classes. Let's have a look at how we can utilize this class to aid us in our scripting.

ServiceNow shortens GlideSystem to gs in scripts so the methods of GlideSystem will be prefixed with gs.

Let's start by seeing how to get a user's sys_id using GlideSystem:

var userID = gs.getUserID();

This puts the logged-in user's sys_id in the userID variable. This can be helpful as you can use this in scripts where you may want to execute different lines of script depending on the attributes of the user. Now we have the user's sys_id, we could use a GlideRecord query to return the fields we desire. ServiceNow does allow us to obtain some of this information in an easier way.

We can get the user object and then use some helpful functions to access further information about the user. To get the user object, we simply write the following:

var userObject = gs.getUser();

Next, let us see some of the most helpful functions:

gs.getUser().getFullName();
gs.getUser().getEmail();
gs.getUser().getLocation();
gs.getUser().getManagerID();
gs.getUser().getCompanyID();

Most of the preceding code is fairly self-explanatory, but the ID functions will return the sys_id of the record in the same way as the user sys_id.

The user's details can be useful for sending notifications and setting up approvals and condition scripts based on locations or companies.

Whilst we are on user data, it is often important to find out which roles a user has to decide what they should have access to. GlideSystem allows us to see whether a user has a certain role in the script, as shown in the following example:

if (gs.hasRole('admin')) {
//Run code for administrators only
}

The hasRole method is especially useful when using script to allow or restrict data to a user. It can also be used to hide or show UI actions that would require an elevated privilege to use. One thing to note is that the hasRole method will always return true for an administrator.

GlideSystem is also good for letting the user know what has happened during a script through an output message for the user. This is done using an Info or Error message. The Info message is displayed in a nice blue box and the Error message, unsurprisingly, in a red one at the top of the form once processing has completed and the next screen has loaded.

Let's have a look at how to script these:

gs.addInfoMessage('Record saved successfully.');
gs.addErrorMessage('Error in script.');

These are fairly simple lines, but are very handy in keeping a user updated with how scripts performed when processed and whether there were any problems. I tend to use the info message to let a user know an action completed successfully when it is not immediately obvious on the screen they are returned to. An Error message is good when something in a script goes wrong. Remember that this message will be displayed to a variety of users so it is best to not get too technical in the message.

GlideSystem also allows us to perform logging so that we can debug our server-side scripts. As server-side scripts are running behind the scenes, we need a way of logging what happened in the script so we can look at it later and review and debug if necessary.

The little bit of script that is the most common for this is log. Simply used, this is just a string of text to send to the logs:

gs.log('Logging Message');

This message will now be visible in the Script Log Statements module in the application navigator. If you are looking through older script in a ServiceNow instance, you will often find these log messages. Sometimes they are commented out in case they are needed again for debugging and sometimes they should have been commented out and the developer forgot.

If many logs are being created, it can be helpful to give your log a unique source so you can more easily search for only logs from your particular source. Let's have a look at what this looks like:

gs.log('Logging Message', 'My Script Log');

This message will then appear in the system log with a source of My Script Log. One thing to note is that it will no longer appear in the Script Log Statements module as you have changed the source to a custom source.

GlideSystem is probably the most useful server-side Glide class and there are more methods on top of the ones we have discussed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.84.155