JSUtil- Global

c_JSUtilAPI.md
Raw file

The JSUtil script include provides shortcuts for common JavaScript routines.

This API is available in global server-side scripts, not scoped scripts.

Parent Topic:Server API reference

JSUtil - doesNotHave(Object item)

Checks if item is null or is undefined.

Name Type Description
item Object The object to check
Type Description
Boolean True if the specified object is null or undefined.
JavaScript
var x = "the quick brown fox";var y = "";var z; gs.print("x = '" + x + "', JSUtil.doesNotHave(x) = " + JSUtil.doesNotHave(x));gs.print("y = '" + y + "', JSUtil.doesNotHave(y) = " + JSUtil.doesNotHave(y));gs.print("z = '" + z + "', JSUtil.doesNotHave(z) = " + JSUtil.doesNotHave(z))

Output:

Shell
x = 'the quick brown fox', JSUtil.doesNotHave(x) = falsey = '', JSUtil.doesNotHave(y) = falsez = 'undefined', JSUtil.doesNotHave(z) = true

JSUtil - escapeAttr(String text)

Escape ampersands commonly used to define URL attributes.

Name Type Description
text String The text
Type Description
String The text with ampersands properly escaped.
JavaScript
var attr = "sysparm_query=active=true&sysparm_view=special"; gs.print(JSUtil.escapeAttr(attr));

Output: This is the returned text. If the text is displayed in the application, the page will render the escaped ampersand with a single ampersand.

Text
sysparm_query=active=true&sysparm_view=special

JSUtil - escapeText(String text)

Escapes invalid XML characters such as "< > &".

Name Type Description
text String The text
Type Description
String The text with escape characters added.
JavaScript
var html = "<b>This is my title</b>"; gs.print(JSUtil.escapeText(html));

Output: This is the value returned. If the result is displayed in the application, the page renders the brackets back so it appears that it is not escaped.

XML
<b>This is my title</b>

JSUtil - getBooleanValue(GlideRecord now_GR, String field)

Returns the value in a boolean GlideRecord field.

Name Type Description
now_GR GlideRecord A GlideRecord
field String The field from which to retrieve the boolean value.
Type Description
Boolean Returns the value in a boolean GlideRecord field, returns true if value of field is true, "true", 1, or "1".
JavaScript
var inc = new GlideRecord("incident");//get an active incidentinc.addActiveQuery();inc.setLimit(1);inc.query();inc.next(); gs.print(JSUtil.getBooleanValue(inc, "active"));

Output: true

JSUtil - has(Object item)

Checks if item is not null and is not undefined.

Name Type Description
item Object The Object to check
Type Description
Boolean True if the specified object is not null and is not undefined.
JavaScript
var x = "the quick brown fox";var y = "";var z; gs.print("x = '" + x + "', JSUtil.has(x) = " + JSUtil.has(x));gs.print("y = '" + y + "', JSUtil.has(y) = " + JSUtil.has(y));gs.print("z = '" + z + "', JSUtil.has(z) = " + JSUtil.has(z));

Output:

Shell
x = 'the quick brown fox', JSUtil.has(x) = truey = '', JSUtil.has(y) = truez = 'undefined', JSUtil.has(z) = false

JSUtil - instance_of(Object item, String class)

Checks to see if the specified object is a member of the specified class.

For JavaScript objects, this method behaves exactly like the JavaScript operator "instanceof", but also supports Java objects.

Name Type Description
item Object The object to check
class String The class to check
Type Description
Boolean True if the specified object is a member of the specified class.
JavaScript
var a = ['a','b','c'];var b = 10;var c = new GlideRecord("incident"); gs.print("JSUtil.instance_of(a,'Array') = " + JSUtil.instance_of(a,Array));gs.print("JSUtil.instance_of(a,'String') = " + JSUtil.instance_of(a,String)); gs.print("JSUtil.instance_of(b,'String') = " + JSUtil.instance_of(b,String)); gs.print("JSUtil.instance_of(c,'GlideRecord') = " + JSUtil.instance_of(c,GlideRecord));

Output:

JavaScript
JSUtil.instance_of(a,'Array') = trueJSUtil.instance_of(a,'String') = falseJSUtil.instance_of(b,'String') = falseJSUtil.instance_of(c,'GlideRecord') = true

JSUtil - isJavaObject(Object value)

Checks if the specified object is a Java class.

Name Type Description
value Object The object to check
Type Description
Boolean True if the specified object is an instance of a Java class.
JavaScript
var tu = new TableUtils("incident");var classes = tu.getHierarchy(); //Java ArrayListvar tables = ["task, incident"]; //JavaScript Array gs.print("JSUtil.isJavaObject(classes) = " + JSUtil.isJavaObject(classes));gs.print("JSUtil.isJavaObject(tables) = " + JSUtil.isJavaObject(tables));

Output:

Text
JSUtil.isJavaObject(classes) = trueJSUtil.isJavaObject(tables) = false

JSUtil - logObject(Object obj, String name)

Logs all the properties in the given object: name, type, and value.

Output is written to the console if you are running from a background script or have debug logging enables. The output is also written to the system log.

Name Type Description
obj Object The object for which to enumerate properties
name String Optional name for the logged object
Type Description
void
JavaScript
var arr = ["a","b","c"]; var inc = new GlideRecord("incident");//get an active incidentinc.addActiveQuery();inc.setLimit(1);inc.query();inc.next(); JSUtil.logObject(arr, "arr");JSUtil.logObject(inc, "inc");

Output:

JavaScript
Log Object: arr  Array of 3 elements    [0]: string = a    [1]: string = b    [2]: string = cLog Object: inc  GlideRecord('incident') @ INC0000002

JSUtil - nil(Object item)

Checks if item is null, undefined, or evaluates to the empty string.

Name Type Description
item Object The object to check
Type Description
Boolean True if the item is null, undefined, or evaluates to the empty string.
JavaScript
var x = "the quick brown fox";var y = "";var z; gs.print("x = '" + x + "', JSUtil.nil(x) = " + JSUtil.nil(x));gs.print("y = '" + y + "', JSUtil.nil(y) = " + JSUtil.nil(y));gs.print("z = '" + z + "', JSUtil.nil(z) = " + JSUtil.nil(z));

Output:

Shell
x = 'the quick brown fox', JSUtil.nil(x) = falsey = '', JSUtil.nil(y) = truez = 'undefined', JSUtil.nil(z) = true

JSUtil - notNil(Object item)

Checks if an item is null, undefined, or evaluates to the empty string.

Note: This method returns an error if the object provided isn’t declared.

Name Type Description
item Object The object to check.

Type

Description

Boolean

Flag that indicates whether the item exists and isn’t empty. An error returns if the object provided isn’t declared.Valid values:

  • true: The object provided has been declared and defined. If the object is a string, it isn’t empty.
  • false: The object provided is null, undefined, or evaluates to an empty string.
The following example shows results for a string variable, an empty string variable, and an undefined variable.
JavaScript
var x = "the quick brown fox";var y = "";var z;
gs.print("x = '" + x + "', JSUtil.notNil(x) = " + JSUtil.notNil(x));gs.print("y = '" + y + "', JSUtil.notNil(y) = " + JSUtil.notNil(y));gs.print("z = '" + z + "', JSUtil.notNil(z) = " + JSUtil.notNil(z));

Output:

Shell
x = 'the quick brown fox', JSUtil.notNil(x) = truey = '', JSUtil.notNil(y) = falsez = 'undefined', JSUtil.notNil(z) = false

JSUtil - toBoolean(Object item)

Converts the specified object to a Boolean.

Name Type Description
item Object The object to convert
Type Description
Boolean If the specified object is a boolean, it is passed through. Non-zero numbers return true. Null or undefined return false. Strings return true only if exactly equal to 'true'.
JavaScript
var zero = 0;var one = 1;var number = 12;var trueBoolean = true;var trueString = "true";var otherString = "random text"; gs.print("JSUtil.toBoolean(zero) = " + JSUtil.toBoolean(zero));gs.print("JSUtil.toBoolean(one) = " + JSUtil.toBoolean(one));gs.print("JSUtil.toBoolean(number) = " + JSUtil.toBoolean(number));gs.print("JSUtil.toBoolean(trueBoolean) = " + JSUtil.toBoolean(trueBoolean));gs.print("JSUtil.toBoolean(trueString) = " + JSUtil.toBoolean(trueString));gs.print("JSUtil.toBoolean(otherString) = " + JSUtil.toBoolean(otherString));

Output:

SQL
JSUtil.toBoolean(zero) = falseJSUtil.toBoolean(one) = trueJSUtil.toBoolean(number) = trueJSUtil.toBoolean(trueBoolean) = trueJSUtil.toBoolean(trueString) = trueJSUtil.toBoolean(otherString) = false

JSUtil - type_of(Object value)

Determines the type of the specified object.

Name Type Description
value Object The object to check

Type

Description

String

The type of the specified object. - 'null' if the given value is null or undefined

  • 'string' if the given value is a primitive string or a String wrapper instance
  • 'number' if the given value is a primitive number or a Number wrapper instance
  • 'boolean' if the given value is a primitive boolean or a Boolean wrapper instance
  • 'function' if the given value is a function
  • 'object' otherwise
``` var a = ["a","b","c"]; var b = 10; var c = new GlideRecord("incident"); var d = true; var e;

gs.print("JSUtil.type_of(a) = " + JSUtil.type_of(a)); gs.print("JSUtil.type_of(b) = " + JSUtil.type_of(b)); gs.print("JSUtil.type_of(c) = " + JSUtil.type_of(c)); gs.print("JSUtil.type_of= " + JSUtil.type_of(d)); gs.print("JSUtil.type_of(e) = " + JSUtil.type_of(e));

Text
Output:

JSUtil.type_of(a) = object JSUtil.type_of(b) = number JSUtil.type_of(c) = object JSUtil.type_of= boolean JSUtil.type_of(e) = null

Text
## JSUtil - unescapeAttr\(String text\)
Restore ampersands from escaped text.
|Name|Type|Description||----|----|-----------||text|String|The text|
|Type|Description||----|-----------||String|The text with escape characters removed.|

var attr = "sysparm_query=active=true&sysparm_view=special";

gs.print(JSUtil.unescapeAttr(attr));

Text
Output:

sysparm_query=active=true&sysparm_view=special

Text
## JSUtil - unescapeText\(String text\)
Removes escape characters.
|Name|Type|Description||----|----|-----------||text|String|The text to process.|
|Type|Description||----|-----------||String|The the text without escape characters.|

var html = "<b>This is my title</b>";

gs.print(JSUtil.unescapeText(html));

Text
Output: This is the value returned. If the text is displayed in the application the page, it renders the html tags and displays the text in bold.

This is my title

Text

Search documentation
Search document titles and file paths on the australia branch