---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# JSUtil - Global

# JSUtil - Global {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 7 minutes to read

The JSUtil script include provides shortcuts for common JavaScript routines.

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

## JSUtil - doesNotHave(Object item) {#ariaid-title2}

Checks if item is null or is undefined.
{#r_JSUtil-doesNotHave_O__table_omt_wg5_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| item | Object | The object to check |
[Table 1. Parameters]

{#r_JSUtil-doesNotHave_O__table_omt_wg5_4t} {#r_JSUtil-doesNotHave_O__table_pmt_wg5_4t__entry__2}

| Type | Description |
|-|-|
| Boolean | True if the specified object is null or undefined. |
[Table 2. Returns]

{#r_JSUtil-doesNotHave_O__table_pmt_wg5_4t}  

    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:

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

## JSUtil - escapeAttr(String text) {#ariaid-title3}

Escape ampersands commonly used to define URL attributes.
{#r_JSUtil-escapeAttr_S__table_p5v_c55_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| text | String | The text |
[Table 3. Parameters]

{#r_JSUtil-escapeAttr_S__table_p5v_c55_4t} {#r_JSUtil-escapeAttr_S__table_q5v_c55_4t__entry__2}

| Type | Description |
|-|-|
| String | The text with ampersands properly escaped. |
[Table 4. Returns]

{#r_JSUtil-escapeAttr_S__table_q5v_c55_4t}  

    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.

    sysparm_query=active=true&sysparm_view=special

## JSUtil - escapeText(String text) {#ariaid-title4}

Escapes invalid XML characters such as "\< \> \&".
{#r_JSUtil-escapeText_S__table_amh_ms5_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| text | String | The text |
[Table 5. Parameters]

{#r_JSUtil-escapeText_S__table_amh_ms5_4t} {#r_JSUtil-escapeText_S__table_bmh_ms5_4t__entry__2}

| Type | Description |
|-|-|
| String | The text with escape characters added. |
[Table 6. Returns]

{#r_JSUtil-escapeText_S__table_bmh_ms5_4t}  

    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.

    <b>This is my title</b>

## JSUtil - getBooleanValue(GlideRecord now_GR, String field) {#ariaid-title5}

Returns the value in a boolean GlideRecord field.
{#r_JSUtil-getBooleanValue_GR_S__table_mtb_ml5_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| now_GR | GlideRecord | A GlideRecord |
| field | String | The field from which to retrieve the boolean value. |
[Table 7. Parameters]

{#r_JSUtil-getBooleanValue_GR_S__table_mtb_ml5_4t} {#r_JSUtil-getBooleanValue_GR_S__table_ntb_ml5_4t__entry__2}

| Type | Description |
|-|-|
| Boolean | Returns the value in a boolean GlideRecord field, returns true if value of field is true, "true", 1, or "1". |
[Table 8. Returns]

{#r_JSUtil-getBooleanValue_GR_S__table_ntb_ml5_4t}  

    var inc = new GlideRecord("incident");
    //get an active incident
    inc.addActiveQuery();
    inc.setLimit(1);
    inc.query();
    inc.next();
     
    gs.print(JSUtil.getBooleanValue(inc, "active"));

Output: true

## JSUtil - has(Object item) {#ariaid-title6}

Checks if item is not null and is not undefined.
{#r_JSUtil-has_O__table_xmc_kg5_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| item | Object | The Object to check |
[Table 9. Parameters]

{#r_JSUtil-has_O__table_xmc_kg5_4t} {#r_JSUtil-has_O__table_ymc_kg5_4t__entry__2}

| Type | Description |
|-|-|
| Boolean | True if the specified object is not null and is not undefined. |
[Table 10. Returns]

{#r_JSUtil-has_O__table_ymc_kg5_4t}  

    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:

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

## JSUtil - instance_of(Object item, String class) {#ariaid-title7}

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.
{#r_JSUtil-instance_of_O_S__table_uh2_s35_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| item | Object | The object to check |
| class | String | The class to check |
[Table 11. Parameters]

{#r_JSUtil-instance_of_O_S__table_uh2_s35_4t} {#r_JSUtil-instance_of_O_S__table_vh2_s35_4t__entry__2}

| Type | Description |
|-|-|
| Boolean | True if the specified object is a member of the specified class. |
[Table 12. Returns]

{#r_JSUtil-instance_of_O_S__table_vh2_s35_4t}  

    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:

    JSUtil.instance_of(a,'Array') = true
    JSUtil.instance_of(a,'String') = false
    JSUtil.instance_of(b,'String') = false
    JSUtil.instance_of(c,'GlideRecord') = true

## JSUtil - isJavaObject(Object value) {#ariaid-title8}

Checks if the specified object is a Java class.
{#r_JSUtil-isJavaObject_O__table_eff_2k5_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| value | Object | The object to check |
[Table 13. Parameters]

{#r_JSUtil-isJavaObject_O__table_eff_2k5_4t} {#r_JSUtil-isJavaObject_O__table_fff_2k5_4t__entry__2}

| Type | Description |
|-|-|
| Boolean | True if the specified object is an instance of a Java class. |
[Table 14. Returns]

{#r_JSUtil-isJavaObject_O__table_fff_2k5_4t}  

    var tu = new TableUtils("incident");
    var classes = tu.getHierarchy(); //Java ArrayList
    var tables = ["task, incident"]; //JavaScript Array
     
    gs.print("JSUtil.isJavaObject(classes) = " + JSUtil.isJavaObject(classes));
    gs.print("JSUtil.isJavaObject(tables) = " + JSUtil.isJavaObject(tables));

Output:

    JSUtil.isJavaObject(classes) = true
    JSUtil.isJavaObject(tables) = false

## JSUtil - logObject(Object obj, String name) {#ariaid-title9}

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.
{#r_JSUtil-logObject_O_S__table_ozw_tr5_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| obj | Object | The object for which to enumerate properties |
| name | String | Optional name for the logged object |
[Table 15. Parameters]

{#r_JSUtil-logObject_O_S__table_ozw_tr5_4t} {#r_JSUtil-logObject_O_S__table_pzw_tr5_4t__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 16. Returns]

{#r_JSUtil-logObject_O_S__table_pzw_tr5_4t}  

    var arr = ["a","b","c"];
     
    var inc = new GlideRecord("incident");
    //get an active incident
    inc.addActiveQuery();
    inc.setLimit(1);
    inc.query();
    inc.next();
     
    JSUtil.logObject(arr, "arr");
    JSUtil.logObject(inc, "inc");

Output:

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

## JSUtil - nil(Object item) {#ariaid-title10}

Checks if item is null, undefined, or evaluates to the empty string.
{#r_JSUtil-nil_O__table_bjp_mh5_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| item | Object | The object to check |
[Table 17. Parameters]

{#r_JSUtil-nil_O__table_bjp_mh5_4t} {#r_JSUtil-nil_O__table_cjp_mh5_4t__entry__2}

| Type | Description |
|-|-|
| Boolean | True if the item is null, undefined, or evaluates to the empty string. |
[Table 18. Returns]

{#r_JSUtil-nil_O__table_cjp_mh5_4t}  

    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:

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

## JSUtil - notNil(Object item) {#ariaid-title11}

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.
{#r_JSUtil-notNil_O__table_trk_f35_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| item | Object | The object to check. |
[Table 19. Parameters]

{#r_JSUtil-notNil_O__table_trk_f35_4t} {#r_JSUtil-notNil_O__table_urk_f35_4t__entry__2}

| 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. {#r_JSUtil-notNil_O__ul_zcy_nr3_myb} |
[Table 20. Returns]

{#r_JSUtil-notNil_O__table_urk_f35_4t}  
The following example shows results for a string variable, an empty string variable, and an undefined variable.

    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:

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

## JSUtil - toBoolean(Object item) {#ariaid-title12}

Converts the specified object to a Boolean.
{#r_JSUtil-toBoolean_O__table_vdm_tk5_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| item | Object | The object to convert |
[Table 21. Parameters]

{#r_JSUtil-toBoolean_O__table_vdm_tk5_4t} {#r_JSUtil-toBoolean_O__table_wdm_tk5_4t__entry__2}

| 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'. |
[Table 22. Returns]

{#r_JSUtil-toBoolean_O__table_wdm_tk5_4t}  

    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:

    JSUtil.toBoolean(zero) = false
    JSUtil.toBoolean(one) = true
    JSUtil.toBoolean(number) = true
    JSUtil.toBoolean(trueBoolean) = true
    JSUtil.toBoolean(trueString) = true
    JSUtil.toBoolean(otherString) = false

## JSUtil - type_of(Object value) {#ariaid-title13}

Determines the type of the specified object.
{#r_JSUtil-type_of_O__table_ic5_lj5_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| value | Object | The object to check |
[Table 23. Parameters]

{#r_JSUtil-type_of_O__table_ic5_lj5_4t} {#r_JSUtil-type_of_O__table_jc5_lj5_4t__entry__2}

| 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 {#r_JSUtil-type_of_O__ul_rjh_sj5_4t} |
[Table 24. Returns]

{#r_JSUtil-type_of_O__table_jc5_lj5_4t}  

    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));

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

## JSUtil - unescapeAttr(String text) {#ariaid-title14}

Restore ampersands from escaped text.
{#r_JSUtil-unescapeAttr_S__table_nqy_t55_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| text | String | The text |
[Table 25. Parameters]

{#r_JSUtil-unescapeAttr_S__table_nqy_t55_4t} {#r_JSUtil-unescapeAttr_S__table_oqy_t55_4t__entry__2}

| Type | Description |
|-|-|
| String | The text with escape characters removed. |
[Table 26. Returns]

{#r_JSUtil-unescapeAttr_S__table_oqy_t55_4t}  

    var attr = "sysparm_query=active=true&amp;sysparm_view=special";
     
    gs.print(JSUtil.unescapeAttr(attr));

Output:

    sysparm_query=active=true&sysparm_view=special

## JSUtil - unescapeText(String text) {#ariaid-title15}

Removes escape characters.
{#r_JSUtil-unescapeText_S__table_j3r_jt5_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| text | String | The text to process. |
[Table 27. Parameters]

{#r_JSUtil-unescapeText_S__table_j3r_jt5_4t} {#r_JSUtil-unescapeText_S__table_k3r_jt5_4t__entry__2}

| Type | Description |
|-|-|
| String | The the text without escape characters. |
[Table 28. Returns]

{#r_JSUtil-unescapeText_S__table_k3r_jt5_4t}  

    var html = "&lt;b&gt;This is my title&lt;/b&gt;";
     
    gs.print(JSUtil.unescapeText(html));

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.

    <b>This is my title</b>


