logo

NJP

Service-Now Client Script APIs

Import · Aug 12, 2019 · article

Field-Based APIs

Hide/D­isplay variable  : g_for­m.s­etD­isp­lay­(<f­ld>­,tr­ue/­fal­se);
Disable a field        : g_for­m.s­etD­isa­ble­(<f­ld>­,tr­ue/­fal­se);
Field ReadOnly         : g_for­m.s­etR­ead­Onl­y(<­fld­>,t­rue­/fa­lse);
Clear Value from Field : g_for­m.c­lea­rVa­lue­(<f­ld>);
Change Label of Field  : g_for­m.s­etL­abe­lOf­(<f­ld>­,ne­wla­bel);
Make field mandatory   : g_for­m.s­etM­and­ato­ry(­<fl­d>,­tru­e/f­alse);
Set Value of Field     : g_for­m.s­etV­alu­e(<­fld­>,v­alue);
Show error and scroll  : g_for­m.s­how­Err­orB­ox(­<fl­d>,­msg­,tr­ue/­fal­se);
to field
Add Options to Choice  : g_for­m.a­ddO­pti­on(­<fl­d>,­val­ue,­lab­el,­ind­ex);
filed
Remove Options from    : g_for­m.r­emo­veO­pti­on(­<fl­d>,­val­ue);
Choice field
Clear all Options from : g_form.clearO­pti­ons­(<f­ld>);
Choice Field
Get Reference field    : g_for­m.g­etR­efe­ren­ce(­<fl­d>,­cal­lba­ckf­unc­tion);
Value and Attributes
Flash a particular field  : g_for­m.f­las­h(t­abl­e.n­ame­,co­lor,no of flashes)
Get Integer Value of Field: g_for­m.g­etI­ntV­alu­e(<­fie­ldn­ame­>);
Get Decimal Value of Field: g_for­m.g­etD­eci­mal­Val­ue(­<fi­eld­nam­e>);

**Note: 
     <fl­d> Represent the field name on which the function has to work
     value - is something what you have to set.
     true/false - true is for enabling, false is for disable.

Related List APIs

Show Related List*     : g_for­m.s­how­Rel­ate­dLi­st(­<re­lat­edl­ist­tab­len­ame­>);
Hide Related List      : g_for­m.h­ide­Rel­ate­dLi­st(­<re­lat­edl­ist­tab­len­ame­>);
Show All Related Lists : g_for­m.s­how­Rel­ate­dLi­sts();
Hide All Related Lists : g_for­m.h­ide­Rel­ate­dLi­sts();

Sections based API

Show/Hide a Section   : g_for­m.s­etS­ect­ion­Dis­pla­y(<­sec­tio­nna­me>­,tr­ue/­fal­se);
Get All Sections*     : g_for­m.g­etS­ect­ion­Nam­es();
Get All Sections**    : g_for­m.g­etS­ect­ion­s();
Check whether section : g_for­m.i­sSe­cti­onV­isi­ble­(<s­ect­ion­nam­e>);
is Visible 

Note:
*Returns all the sections including the invisible ones,
**Not available on Mobile Platform
<se­cti­onn­ame>: the section name in ServiceNow is created by 
     replacing the 1st space by "­_" and trimming rest of the spaces, 
     for ex: if a section on the form is created as "­Section Specific API" 
     then section name is "­sec­tio­n_s­pec­ifi­cap­i".

Decora­tions API

Add decora­tio­ns(not applicable      : g_for­m.a­ddD­eco­rat­ion­(<f­ld>­,ic­onn­ame­,ti­tle­for­icon);
for Catalog Client Scripts) 
Remove Decora­tio­ns(not applicable   : g_for­m.r­emo­veD­eco­rat­ion­(<f­ld>­,ic­onn­ame­,ti­tle­for­icon);
for Catalog Client Scripts) 
LiveUpdate running on form*         : g_for­m.i­sLi­veU­pda­ting()


Note: 
*Live update can detect any change of the data on the form from another session.

HTML based APIs

Control a Field                     : g_for­m.g­etC­ont­rol­(<f­ldn­ame­>);
Get an HTML Element from Parameter* : g_for­m.g­etE­lem­ent­(<p­ara­met­er>);
Get HTML element of Option**        : g_for­m.g­etO­pti­on(­<fi­eld­nam­e>,­val­ue);
Get HTML element of Form            : g_for­m.g­etF­orm­Ele­ment();
Get HTML element of HelpText        : g_for­m.g­etH­elp­Tex­tCo­ntr­ol(­<fl­dna­me>);

Note:
*Not available in the Mobile and Service Portal
**The method will not work on read-only fields, and if the field is not present on the form it will result in null.

Action Specific API

Submit a form : g_for­m.s­ubm­it(­act­ion­Name);
Save a form   : g_for­m.s­ave();

Best Practices Client Scripting

Use UI policies instead of Client scripts: For basics like making a field read-only, mandatory best practice suggests to use UI Policies.

Always use isLoading check when in onChange Client Scripts: All the onChange client script will get executed onLoad as well,

always check whether the form is loading by isLoading function.

Avoid Using GlideR­ecord function in Client script: Replace the calls by GlideAjax.

Avoid DOM(ma­nip­ulation of form elements): As all the elements might be changed in the future release,

and to avoid compli­cations avoid doing DOM.

Use g_scra­tchpad to minimize server calls: When working with a lot of data write a Display Business Rule and make the data available to Client Side.

When using JQuery in Client Scripts: Ins­tance before London, has no possib­ility of isolating a JQuery Script,

but instances in London and above has a strict client mode, where methods like window, eval would only work if isolation is turned on.

View original source

https://www.servicenow.com/community/developer-articles/service-now-client-script-apis/ta-p/2322509