Client Scripting - fdlGitHub/ServiceNow GitHub Wiki


Field is present on a form

g_form.getControl('<Field_Name>')

Field is visible on a form

g_form.isDisplayNone(g_form.getGlideUIElement('<Field_Name>'), g_form.getControl('<Field_Name>'));

Field is editable on a form

g_form.isEditableField(g_form.getGlideUIElement('<Field_Name>'), g_form.getControl('<Field_Name>'));

Set reference fields

To limit client/server communication and keeping a good user experience (prevent performance issue), when you set the value of a reference field, set also the display value.

g_form.setValue('<Field_Name>', <SYS_ID>, <Display_Value>);

Set GlideList fields

g_form.setValue('<Field_Name>', '<SYS_ID_1>,<SYS_ID_2>', '<DISPLAY_VALUE_1>,<DISPLAY_VALUE_2>');

Get the display value of a reference field

g_form.getDisplayBox('<Field_Name>').value

Manage annotation into tabs

To display or hide Annotations present in a tab, use the following syntax:

// Hide title on a form
$j($j('[tab_caption="<TAB_NAME>"]').find('.annotation-wrapper')[<ANNOTATION_INDEX>]).css('display', <Display Value>);

g_scratchpad or how to limit communications Client/Server

g_scratchpad is a pre-defined object shared from the server to the client inform context, in order to transfer data without having to perform AJAX call once the client form is loaded

Define Business Rule

In a Business Rule set as Advanced, select display as When this Business Rule should run.

(function executeRule(current, previous /*null when async*/) {
    
    g_scratchpad.my_field_name = {
        'value': current.getValue('my_field_name');
        'display_value': current.getDisplayValue('my_field_name');
    }
    
    g_scratchpad.isMemberOfGroup1 = gs.getUser().isMemberOf('support_group_1');
    
})(current, previous);

Define Client Script

In Client Scripts typed onLoad, onChange or onSubmit, you can use the g_scratchpad object to read or set data

function onLoad() {
    // When user is not member of the Group 1, the assigned to field should be in ReadOnly mode
    if(!g_scratchpad.isMemberOfGroup1) {
        g_form.setReadOnly('assigned_to', true);
    }
}

Limit number of Client Scripts

To limit client/server communication and keeping a good user experience (prevent performance issue), don't write multiple client scripts with multiple server call (GlideAjax, g_form.getReference, etc...) and use a UI Script.

When you use the g_form.getReference method, keep the result in a global variable of the UI Script and re-use this variable while the reference field is not updated.

Define your UI Script

Create a UI Script like a Script Include

var UIScriptName = Class.create();

UIScriptName.prototype = {
    initialize: function() {
    },
    
    testFunc: function() {
        alert('testFunc in UIScriptName');
    },
    
    type: 'UIScriptName'
};

// Initilialise UIScriptName object
UIScriptName = new UIScriptName();

Load the UI Script when the form is loaded :

Create a Client Script on load and add following lines. Don't forgot to update the v1 parameter at each version you want deployed on other instances.

// Change the parameter v1 for each new release of the UI Script UIScriptName
// For a development phase, clear the cache to can use the last version of the UI Script
ScriptLoader.getScripts('UIScriptName.jsdbx?v1', function() {});

Service Now Documentation : ScriptLoader

Run a function when a field change

Create an onChange Client Script and call the function where all the to do is present.

// Use the UIScriptName UI Script loaded by the OnLoad Client Script
if(typeof UIScriptName != "undefined") {
    UIScriptName.testFunc();
}

Check eMail entry

Use the following method to check if the eMail entry have the correct syntax.

isEmailValid('<eMail_address>')
⚠️ **GitHub.com Fallback** ⚠️