Skip to main content

ServiceNow Glide API's Examples

· 3 min read
Kaustubh Kulkarni

ServiceNow Glide APIs provide developers with a set of RESTful APIs to interact with ServiceNow and build custom applications. The Glide APIs support a range of functions for creating, updating, and querying data in ServiceNow. In this section, we'll explore the different Glide APIs available in ServiceNow and provide examples of how they can be used.

  1. GlideRecord API:

The GlideRecord API is one of the most widely used APIs in ServiceNow. It provides developers with a way to interact with tables and records in ServiceNow. With the GlideRecord API, developers can query and manipulate data in ServiceNow tables. Here's an example of how to use the GlideRecord API to query data:

// Query data from the Incident table
var gr = new GlideRecord('incident');
gr.addQuery('priority', 1);
gr.query();
while (gr.next()) {
gs.log(gr.number);
}

In this example, we're querying the incident table for all records with a priority of 1. We then loop through the results and log the incident number.

  1. GlideAggregate API:

The GlideAggregate API provides developers with a way to perform aggregate functions on data in ServiceNow tables. With the GlideAggregate API, developers can perform functions like sum, count, min, and max on data in ServiceNow tables. Here's an example of how to use the GlideAggregate API to perform a count on data:

// Count the number of incidents by category
var ga = new GlideAggregate('incident');
ga.addAggregate('COUNT', 'category');
ga.groupBy('category');
ga.query();
while (ga.next()) {
gs.log(ga.category + ': ' + ga.getAggregate('COUNT', 'category'));
}

In this example, we're using the GlideAggregate API to count the number of incidents by category. We group the results by category and then loop through the results and log the category and count.

  1. GlideSystem API:

The GlideSystem API provides developers with a way to interact with system-level functions in ServiceNow. With the GlideSystem API, developers can access system properties, log messages, and perform other system-level functions. Here's an example of how to use the GlideSystem API to log a message:

// Log a message to the system log
gs.log('Hello World');

In this example, we're using the GlideSystem API to log a message to the system log. This is a useful function for debugging and troubleshooting.

  1. GlideForm API:

The GlideForm API provides developers with a way to interact with forms in ServiceNow. With the GlideForm API, developers can access form fields, submit forms, and perform other form-level functions. Here's an example of how to use the GlideForm API to access a form field:

// Get the value of the Short Description field on the current form
var shortDesc = g_form.getValue('short_description');
gs.log(shortDesc);`

In this example, we're using the GlideForm API to get the value of the Short Description field on the current form. We then log the value to the system log.

  1. GlideAjax API:

The GlideAjax API provides developers with a way to make asynchronous server calls in ServiceNow. With the GlideAjax API, developers can make server calls without refreshing the page. Here's an example of how to use the GlideAjax API to make a server call:

// Make an asynchronous server call to get the current user's name
var ga = new GlideAjax('GetUserName');
ga.addParam('sysparm_name', 'getCurrentUserName');
ga.getXML(function(response) {
var name = response.responseXML.documentElement.getAttribute('answer');
gs.log(name);
});