2

JavaScript Arrays, Methods & Objects: ServiceNow Scripting Guide

Working in ServiceNow means managing and operating with large amounts of data and values. Arrays manage groups of objects and lists of values, along with data and actions. 

In this topic, we will be focusing on Arrays, their methods, along with objects and classes in JavaScript. 

JavaScript Arrays

An array can store different types of values in sequential order, and its size can change dynamically. The values you add are called elements, and each element has a position called an index. 

You create an array with square brackets [ ].

Also, if you add or collect values from a GlideRecord query, you can also leave the array empty and fill it later. For example,

var sysIds = [];
var gr = new GlideRecord('incident');
gr.addQuery('priority', 1);
gr.query();
while (gr.next()) {
  sysIds.push(gr.getUniqueValue());
}
gs.info('Found ' + sysIds.length + ' incidents');

Array Manipulation methods

– toString(): Returns array elements separated with commas. 

Example: 

function name(){ 

var products = ["TV", "Mobile", "Shoe"]; 

console.log(products.toString()); 

} 

name();


– join(): Returns array elements separated with a custom delimiter. 

Example: 

function f1(){ 

var products = ["TV", "Mobile", "Shoe"]; 

console.log(products.join("_")); 

} 

f1();


– slice(): Returns array elements between indexes. 

Example: 

function f1(){ 

var products = ["TV", "Mobile", "Shoe"]; 

console.log(products.slice(1,2)); 

} 

f1();

Adding and removing elements from Array

– push(): Add new elements as the last item. 

Example: 

function push(){ 

var fruits = ["Banana","Orange","Mango"] 

fruits.push("apple");

console.log(fruits); 

}push();


– unshift(): Add new elements as the first item. 

Example: 

function unshift(){ 

var fruits = ["Banana","Orange","Mango"] 

fruits.unshift("apple"); 

console.log(fruits); 

}unshift();


– pop(): Remove the last item. 

Example: 

function pop(){ 

var fruits = ["Banana","Orange","Mango"] 

fruits.pop(); 

console.log(fruits); 

}pop();


– shift(): Removes and returns the first item. 

Example: 

function shift(){ 

var fruits = ["Banana","Orange","Mango"]; 

console.log(fruits); 

fruits.shift(); 

console.log(fruits); 

}shift();


– splice(): It is used to add or remove items at any specific index. 

Syntax: 

splice(startIndex, removeCount, NewItems…). 

For Example: 

let fruits = ["Apple", "Banana", "Mango", "Orange"];

fruits.splice(1, 2);

console.log(fruits);

ServiceNow tip: Some values in ServiceNow, like list fields, are returned as comma-separated strings. Use split(‘,’) to turn them into an array, and join(‘,’) to turn an array back into a string.

JavaScript Objects

Object-oriented programming or OOP concepts in JavaScript are prototype-based. It helps developers achieve reusability and security. 

An Object in JavaScript is an entity that contains properties and methods. It keeps all related data and functionality under one memory reference.

  • Data is stored in properties. 
  • Functionality is defined in methods. 

Syntax: 

var object = { 

property: value, 

method: function(){} 

}

JavaScript Class 

Classes are blueprints for objects and act as templates for multiple objects. They are configured by using the “class” keyword. A typical JavaScript class comprises of: 

  • Properties 
  • Methods 
  • Constructor 

Syntax: (ES6 class syntax doesn’t work in classic server-side scripts and throws an error.)

class ClassName { 

//members; 

}

Syntax: (This syntax always works)

var IncidentHelper = Class.create();

IncidentHelper.prototype = {

initialize: function(gr) {

this.gr = gr;

},

isHighPriority: function() {

return parseInt(this.gr.getValue('priority'), 10) <= 2;

},

type: 'IncidentHelper'

};

// Usage

var helper = new IncidentHelper(current);

if (helper.isHighPriority()) {

gs.info('High priority: ' + current.number);

}

What’s Next?

Now that you understand how to store lists and structure data using JavaScript, in the next topic we will be covering how to use regular expressions to validate text. It will also cover how to convert data to and from JSON.

Next Topic

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

SALE IS LIVE
50% OFF