ServiceNow Certification Mock Exams
Click To View Now
SNOWYCODE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
How To Use gs.log In ServiceNow
How To's

How To Use gs.log In ServiceNow

How To Use gs.log In ServiceNow

Intro to gs.log

In ServiceNow, GSLog is a script include that provides a way to write messages to the Log [syslog] whenever a server-side script is executed. This is very useful for when you are working in server-side code and need to debug the code.

In a client-side script, you'd usually just add an info message with the values or variables that you're attempting to debug. However, when you're server-side, you can't do that. This is where GSLog comes in to help!

GSLog is called in a script by using the gs.log() method as a line of code, and is callable in any server-side Global object, including (but not limited to) Business Rules, Script Includes, server-side UI Actions, Scheduled Jobs, and Fix Scripts.

Using gs.log()

Below are the parameters for gs.log():

 gs.log("message", "source")

message: the message (string or variables) to be recorded to the system log, contained within parenthesis. Appears in the "Message" column of the Log [syslog] entry.

source (optional): a string of text to identify the log entry, also contained within parenthesis. Although an optional parameter, it's recommended to include a source it will appear in the "Source" column of the Log [syslog] entry, making it much easier to locate the entry.

Below is an example GSLog script:

 gs.log("This is a test log message!", "MyScript")

When initiated, the above line of script will create a single record in the Log [syslog] table

Example Scenario - Log A Message When A Scheduled Job Completes

In the scenario below, we have created a custom scoped application with a "Snowy Tickets" table (extended from the Tasks table) with the system name [x_547261_snowyco_0_snowy_tickets]. Along with the custom table is a Global scheduled job, that when executed will query the Snowy Tickets table for records with a status of Open ('1'), then sets all retrieved records with this status to Work in Progress ('2'). Below is the script from the scheduled job:

 var tableName = 'x_547261_snowyco_0_snowy_tickets';
var statusField = 'state';

var grSnowyTickets = new GlideRecord(tableName);
grSnowyTickets.addQuery(statusField, '1');
grSnowyTickets.query();

if (grSnowyTickets.next()) {
    grSnowyTickets.setValue(statusField, '2');
    grSnowyTickets.update();
    
} else {

}

However, we would like to log whenever the scheduled job is executed, and well as log additional information such as how many records the query retrieved, and which specific records were updated. (if any) To accomplish this, we've modified the script to include four gs.log() lines:

 var tableName = 'x_547261_snowyco_0_snowy_tickets';
var statusField = 'state';

// Log the start of execution
gs.log("Starting to query records from table: " + tableName, "MyScript");

var grSnowyTickets = new GlideRecord(tableName);
grSnowyTickets.addQuery(statusField, '1');
grSnowyTickets.query();

// Log how many records were found
gs.log(grSnowyTickets.getRowCount() + " records found with status 'Open'", "MyScript");

if (grSnowyTickets.next()) {
    grSnowyTickets.setValue(statusField, '2');
    grSnowyTickets.update();

    // Log the update
    gs.log("Updated record sys_id: " + grSnowyTickets.getUniqueValue() + " to status 'Work in Progress'", "MyScript");
} else {
    // Log if no matching records were found
    gs.log("No records found with status 'Open'", "MyScript");
}

When executed, the scheduled job will create a minimum of three log records in the Log table, depending on how many records are retrieved. All records will have Source = MyScript (as this is the value we defined as the source parameter in all gs.log() lines) as well as Level = Information. The Message values values will be the following:

Entry 1: Starting to query records from table: x_547261_snowyco_0_snowy_tickets

Entry 2: <number> records found with status 'Open'

Entry 3: Updated record sys_id <sys_id of record> to status 'Work in Progress

Alternate Entry 3 if no records found: No records found with status 'Open'

Viewing the System Logs

To view the Log table, perform the following:

1) Using the Filter Navigator, navigate to System Logs > All.

2) The Log table [syslog] is displayed. The default columns are Created, Level, Message, and Source.

Note: Log records created using gs.log() will always have Level = Information.

The (System) Log table
The (System) Log table

3) To easily locate the log records created by the scheduled job execution, perform the following, run a filter on the Source column for MyScript.

Note: by default the Logs table automatically adds a filter for "Created Today", so disable this filter if needed (just a heads up though, depending on your instance, disabling that filter could cause the table to load for multiple minutes)

 Log entries from the scheduled job
Log entries from the scheduled job

Limitations Of gs.log

As mentioned earlier in this article, gs.log() will only function on a Global script, although the script can still query tables from custom scoped applications such as in the scenario above: the scheduled job is Global, but the [x_547261_snowyco_0_snowy_tickets] table that was queried is from custom scoped application.

For scripts within custom scoped applications, gs.info() must be used instead. This method utilizes the exact same parameters as gs.log().

Conclusion

The GSLog script include provides a useful tool for ServiceNow developers by allowing the logging of messages from server-side, Global scripts. Developers will often utilize it for debugging or tracking purposes. We hope this guide has helped you learn more about it and you can add it to your ServiceNow development arsenal!

Looking for something else? Check out our other Cheat Sheets below!

Snowycode team
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Read more