Debugging ServiceNow Scripts: gs.log, gs.debug, and the Script Debugger

Debugging in ServiceNow is not as obvious as in most development environments. There is no console.log that shows up anywhere useful. Here is every tool available and when to use each one.

gs.log() — the basics

gs.log() writes to the System Log (System > System Log > All). This is your first line of debugging for server-side scripts:

gs.log('Value of state: ' + current.getValue('state'));
gs.log('Record sys_id: ' + current.sys_id);

View logs at System > System Log > All and filter by Source (usually the script name) or message content.

Log levels

gs.info('Normal operation message');
gs.warn('Something unexpected but non-fatal');
gs.error('Something failed — check this');

In production instances, debug logs are suppressed. Use gs.info() or gs.log() for messages you want to see in production.

gs.debug() with session debug

Turn on session-level debug logging at System Diagnostics > Session Debug > Log. Then gs.debug() messages appear in the session log — useful for tracing execution flow without cluttering the system log.

The Script Background page

Navigate to System Definition > Scripts - Background. You can run any server-side JavaScript here interactively and see output immediately. Invaluable for testing logic before putting it in a Business Rule:

var gr = new GlideRecord('incident');
gr.addEncodedQuery('active=true^state=1');
gr.setLimit(5);
gr.query();
while (gr.next()) {
    gs.log(gr.number + ': ' + gr.short_description);
}

The Script Debugger

The interactive Script Debugger works like a real IDE debugger — set breakpoints, step through code, inspect variable values. Access it at System Diagnostics > Script Debugger.

To use it: open the debugger, check "All scripts" or set specific filters, then trigger the script (save a record, load a form, etc.). Execution pauses at breakpoints and you can inspect the call stack.

Try/catch for graceful error capture

try {
    var gr = new GlideRecord('incident');
    gr.get(sys_id);
    // ... do something
} catch (e) {
    gs.error('Error in MyScript: ' + e.message + ' | Stack: ' + e.stack);
}

Checking if a query returned results

var gr = new GlideRecord('incident');
gr.addQuery('number', 'INC0000001');
gr.query();
if (!gr.next()) {
    gs.warn('No record found for INC0000001');
    return;
}

Want the complete reference?

This article is part of the NowSpectrum knowledge library. Browse all products for cheat sheets, interview prep, and deep-dive reference guides.

Browse All Products →