This article is a supplement to the ServiceNow documentation. For full documentation please refer ServiceNow official website
Checkout our NEW Video Channel you can like and subscribe too!

Introduction

The GlideSystem (referred to by the variable name gs in Business Rules) provides a number of convenient methods to get information about the system, the current logged in user, etc.

EventQueue

Queues an event for the event manager. Parameters:

  • Name of the event being queued.
  • A GlideRecord object, such as current.
  • An optional parameter, saved with the instance if specified.
  • A second optional parameter, saved with the instance if specified.
  • An event queue to add the event to.
if (current.operation() != insert && current.comments.changes()) {
    gs.eventQueue(incident.commented, current, gs.getUserID(),
        gs.getUserName());
}

Get Display Column

Gets the display column for the table.

Parameters:

  • Name of the event being queued.
  • A GlideRecord object, such as current.
  • An optional parameter, saved with the instance if specified.
  • A second optional parameter, saved with the instance if specified.
  • An event queue to add the event to.
// Return the sys_id value for a given table and its display value
function GetIDValue(table, displayValue) {
    var rec = new GlideRecord(table);
    var dn = gs.getDisplayColumn(table);
    if (rec.get(dn, displayValue))
        return rec.sys_id;
    else
        return null;
}

Get Property

Gets the value of a Glide property. If the property is not found, return an alternate value.

//Check for attachments and add link if there are any
var attachment_link = '';
var rec = new GlideRecord('sc_req_item');
rec.addQuery('sys_id', current.request_item);
rec.query();
if (rec.next()) {
    if (rec.hasAttachments()) {
        attachment_link = gs.getProperty('glide.servlet.uri') +
            rec.getLink();
    }
}

log

Logs a message to the system log and saves it to the syslog table.

Parameters:

  • String message – message to log, for the log’s Message field.
  • String source – (optional) the source of the message, for the log’s Source field.
  var count = new GlideAggregate('incident');
  count.addQuery('active', 'true');
  count.addAggregate('COUNT', 'category');
  count.query();
  while (count.next()) {
      var category = count.category;
      var categoryCount = count.getAggregate('COUNT', 'category');
      gs.log("The are currently " + categoryCount + " incidents with a
          category of " + category, "
          Incident Counter ");
      }

nil

Queries an object and returns true if the object is null or contains an empty string.

if ((!current.u_date1.nil()) && (!current.u_date2.nil())) {
    var start = current.u_date1.getGlideObject().getNumericValue();
    var end = current.u_date2.getGlideObject().getNumericValue();
    if (start > end) {
        gs.addInfoMessage('start must be before end');
        current.u_date1.setError('start must be before end');
        current.setAbortAction(true);
    }
}

print

Writes a message to the system log. This method does not write the message to the syslog table unless debug has been activated.

var rec = new GlideRecord('incident');
rec.addQuery('active', false);
rec.query();
while (rec.next()) {
    gs.print('Inactive incident ' + rec.number + ' deleted');
    rec.deleteRecord();
}

dateDiff

Calculates the difference between two dates. This method expects the earlier date as the first parameter and the later date as the second parameter; otherwise, the method returns the difference as a negative value.

Use getDisplayValue() to convert the strings to the expected format.

Parameters:

  • startDate – a starting date to compare, in the current user’s date format.
  • endDate – an ending date to compare, in the current user’s date format.
  • boolean bnumericValue – true to return difference in number of seconds as a string, false to return difference in the format ddd hh:mm:ss.
// Given two date/times as DateTime objects
// Set the values this way to ensure a consistent input time
var date1 = new GlideDateTime();
var date2 = new GlideDateTime();
date1.setDisplayValueInternal('2014-01-01 12:00:00');
date2.setDisplayValueInternal('2014-01-01 13:00:00');
// Determine the difference as number of seconds (returns a string)
// Use getDisplayValue() to convert the string to the format expected
// by dateDiff()
var diffSeconds = gs.dateDiff(date1.getDisplayValue(),
    date2.getDisplayValue(), true);
// JavaScript will coerce diffSeconds from a string to a number
// since diffSeconds is being compared to a number
var msg = (diffSeconds <= 0) ? ' is on or after ' : ' is before ';
gs.print(date1.getDisplayValue() + msg + date2.getDisplayValue())

Days Ago

Gets a date and time for a certain number of days ago.

function contractNoticeDue() {
    var gr = new GlideRecord("contract");
    gr.addQuery("u_contract_status", "Active");
    gr.query();
    while (gr.next()) {
        if ((gr.u_termination_date <= gs.daysAgo(-90)) 
        && (gr.u_contract_duration == "Long")) {
            gr.u_contract_status = "In review";
        } else if ((gr.u_termination_date <= gs.daysAgo(-50)) 
        && (gr.u_contract_duration == "Medium")) {
            gr.u_contract_status = "In review";
        } else if ((gr.u_termination_date <= gs.daysAgo(-10)) 
        && (gr.u_contract_duration == "Short")) {
            gr.u_contract_status = "In review";
        }
    }
    gr.update();
}

Now Date Time

The current date and time in the user-defined format.

current.u_target_date = gs.nowDateTime();
current.variables.end_date.setDisplayValue(gs.nowDateTime());

Add Info Message

Adds an info message for the current session. Can be called using getInfoMessages(). Note: This method is not supported for asynchronous business rules.

if ((!current.u_date1.nil()) && (!current.u_date2.nil())) {
    var start = current.u_date1.getGlideObject().getNumericValue();
    var end = current.u_date2.getGlideObject().getNumericValue();
    if (start > end) {
        gs.addInfoMessage('start must be before end');
        current.u_date1.setError('start must be before end');
        current.setAbortAction(true);
    }
}

Get Roles

Gets a list of roles for the current user. The list of roles does not reflect any changes made during the current user session. To get the updated list of roles, the user must log out and log back in.

gs.print(gs.getSession().getRoles());

Get Session

Gets a reference to the current Glide session.

if (!gs.hasRole("admin") && !gs.hasRole("user_admin") &&
    gs.getSession().isInteractive()) {
    current.addQuery("active", "true");
}

Get User ID()

Returns the sys_id of the current user.

if (current.operation() != insert && current.comments.changes()) {
    gs.eventQueue(incident.commented, current, gs.getUserID(),
        gs.getUserName());
}

Get Session

Gets a reference to the current Glide session.

if (!gs.hasRole("admin") && !gs.hasRole("user_admin") &&
    gs.getSession().isInteractive()) {
    current.addQuery("active", "true");
}

Date and Time Functions

beginningOfLastMonth()// Gets the date and time for the beginning of last month in GMT.
beginningOfLastWeek()// Gets the date and time for the beginning of last week in GMT.
beginningOfNextWeek() // Gets the date and time for the beginning of next week in GMT.
beginningOfNextMonth() // Gets the date and time for the beginning of next month in GMT.
beginningOfNextYear() // Gets the date and time for the beginning of next year in GMT.
beginningOfThisMonth() // Gets the date and time for the beginning of this month in GMT.
beginningOfThisQuarter() // Gets the date and time for the beginning of this quarter in GMT.
beginningOfThisWeek() // Gets the date and time for the beginning of this week in GMT.
beginningOfThisYear() // Gets the date and time for the beginning of this week in GMT.
beginningOfToday() // Gets the date and time for the beginning of today in GMT.
beginningOfYesterday() // Gets the date and time for the beginning of yesterday in GMT.
calDateDiff(String, String,boolean)//Calculate the difference between two dates using the default calendar.
dateDiff(String, String, boolean) // Calculates the difference between two dates.
dateGenerate(String, String) // Generates a date and time for the specified date.
daysAgo(int) //Gets a date and time for a certain number of days ago.
daysAgoEnd(int) //Gets a date and time for end of the day a certain number of days ago.
daysAgoStart(int) //Gets a date and time for beginning of the day a certain number of days ago.
endOfLastMonth() // Gets the date and time for the end of last month in GMT.
endOfLastWeek() // Gets the date and time for the end of last week in GMT, in the format yyyy-mm-dd hh:mm:ss.
endOfLastYear() // Gets the date and time for the end of last year in GMT.
endOfNextMonth() // Gets the date and time for the end of next month in GMT.
endOfNextWeek() // Gets the date and time for the end of next week in GMT.
endOfNextYear() // Gets the date and time for the end of next year in GMT.
endOfThisMonth() // Gets the date and time for the end of this month in GMT.
endOfThisQuarter() // Gets the date and time for the end of this quarter in GMT.
endOfThisWeek() // Gets the date and time for the beginning of this week in GMT.
endOfThisYear() // Gets the date and time for the end of this year in GMT.
endOfToday() // Gets the date and time for the end of today in GMT.
endOfYesterday() // Gets the date and time for the end of yesterday in GMT.
hoursAgo(int) //Gets a date and time for a certain number of hours ago.
hoursAgoEnd(int) //Gets a date and time for the end of the hour a certain number of hours ago.
hoursAgoStart(int) //Gets a date and time for the start of the hour a certain number of hours ago.
lastWeek() // Date and time one week ago in GMT.
minutesAgo(int) //Gets a date and time for a certain number of minutes ago.
minutesAgoEnd(int) //Gets a date and time for the end of the minute a certain number of minutes ago.
minutesAgoStart(int) //Gets a date and time for a certain number of minutes ago.
monthsAgo(int) //Gets a date and time for a certain number of months ago.
monthsAgoEnd(int) //Gets a date and time for the last day of the month a certain number of months ago.
monthsAgoStart(int) //Gets a date and time for the first day of the month a certain number of months ago.
now() // Gets the current date.
nowNoTZ() // Gets the current GMT date time.
nowDateTime() // Gets the current date and time.
quartersAgo(int) //Gets a date and time for a certain number of quarters ago.
quartersAgoEnd(int) //Gets a date and time for the last day of the quarter a certain number of quarters ago.
quartersAgoStart(int) //Gets a date and time for the first day of the quarter a certain number of quarters ago.
yearsAgo(int) //Gets a date and time for a certain number of years ago.
yesterday() // Gets yesterday's time.
isFirstDayOfMonth(Object) //Checks whether the date is the first day of the month.
isFirstDayOfWeek(Object) //Checks whether the date is the first day of the week. This uses the ISO standard of Monday being the first day of
the week.
isFirstDayOfYear(Object) //Checks whether the date is the first day of the year
isLastDayOfMonth(Object) //Checks whether the date is the last day of the month.
isLastDayOfWeek(Object) //Checks whether the date is the last day of the week.
isLastDayOfYear(Object) //Checks whether the date is the last day of the year.

Conclusion

That’s all for now. Hope these queries will give you some idea on CRUD operation in ServiceNow. Have a look at advance glide scripts too.

    Content