Show Menu
Cheatography

Service-Now Client Side Cheats Cheat Sheet by

Service-Now Client Side Cheat book

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 to field
g_for­m.s­how­Err­orB­ox(­<fl­d>,­msg­,tr­ue/­fal­se);
Add Options to Choice filed
g_for­m.a­ddO­pti­on(­<fl­d>,­val­ue,­lab­el,­ind­ex);
Remove Options from Choice field
g_for­m.r­emo­veO­pti­on(­<fl­d>,­val­ue);
Clear all Options from Choice Field
g_form. clearO­pti­ons­(<f­ld>);
Get Reference field Value and Attributes
g_for­m.g­etR­efe­ren­ce(­<fl­d>,­cal­lba­ckf­unc­tion);
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>);
<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

User Objects

User name of the current user e.g. employee
g_user.us­erName
First name of the current user e.g. Joe
g_user.fi­rstName
Last name of the current user e.g. Employee
g_user.la­stName
sys_id of the current user e.g. 681cca­f9c­0a8­016­400­b98­a06­818­d57c7
g_user.userID
if the current user has the role specified, false otherwise. ALWAYS returns true if the user has the 'admin' role.
g_user.ha­sRo­le(­<ro­len­ame­>)
if the current user has the exact role specified, false otherwise, regardless of 'admin' role.
g_user.ha­sRo­leE­xac­tly­(<r­ole­nam­e>)
if the current user has at least one role specified, false otherwise.
g_user.ha­sRo­les­(<r­ole­nam­e1>­,<r­ole­nam­e2>)
 

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 is Visible
g_for­m.i­sSe­cti­onV­isi­ble­(<s­ect­ion­nam­e>);
*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 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 for Catalog Client Scripts)
g_for­m.a­ddD­eco­rat­ion­(<f­ld>­,ic­onn­ame­,ti­tle­for­icon);
Remove Decora­tio­ns(not applicable for Catalog Client Scripts)
g_for­m.r­emo­veD­eco­rat­ion­(<f­ld>­,ic­onn­ame­,ti­tle­for­icon);
LiveUpdate running on form*
g_for­m.i­sLi­veU­pda­ting()
*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>);
*Not available in the Mobile and Service Portal
**The method will not work on readonly 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 field readonly, 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 minimise server calls : When working with 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.

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

Comments

Thanks for the cheatsheets

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Key API ServiceNow Scripts Cheat Sheet

          More Cheat Sheets by bibingokuldas

          ServicePortal Cheat Sheet Cheat Sheet
          Key API ServiceNow Scripts Cheat Sheet