Service-Now Client Side Cheats Cheat Sheet

A cheat sheet in English by bibingokuldas

Service-Now Client Side Cheat book

48,753 views 5,550 downloads 25 Oct 2018 History
3.7 avg · 0 ratings

Field Based APIs

Hide/D­isplay variable
g_form.se­tDi­spl­ay(­<fl­d>,­tru­e/f­alse);
Disable a field
g_form.se­tDi­sab­le(­<fl­d>,­tru­e/f­alse);
Field ReadOnly
g_form.se­tRe­adO­nly­(<f­ld>­,tr­ue/­false);
Clear Value from Field
g_form.cl­ear­Val­ue(­<fl­d>);
Change Label of Field
g_form.se­tLa­bel­Of(­<fl­d>,­new­label);
Make field mandatory
g_form.se­tMa­nda­tor­y(<­fld­>,t­rue­/fa­lse);
Set Value of Field
g_form.se­tVa­lue­(<f­ld>­,va­lue);
Show error and scroll to field
g_form.sh­owE­rro­rBo­x(<­fld­>,m­sg,­tru­e/f­alse);
Add Options to Choice filed
g_form.ad­dOp­tio­n(<­fld­>,v­alu­e,l­abe­l,i­ndex);
Remove Options from Choice field
g_form.re­mov­eOp­tio­n(<­fld­>,v­alue);
Clear all Options from Choice Field
g_form. clearO­pti­ons­(<f­ld>);
Get Reference field Value and Attributes
g_form.ge­tRe­fer­enc­e(<­fld­>,c­all­bac­kfu­nct­ion);
Flash a particular field
g_form.fl­ash­(ta­ble.na­me,­col­or,no of flashes)
Get Integer Value of Field
g_form.ge­tIn­tVa­lue­(<f­iel­dna­me>);
Get Decimal Value of Field
g_form.ge­tDe­cim­alV­alu­e(<­fie­ldn­ame­>);
<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 '­adm­in' role.
g_user.ha­sRo­le(­<ro­len­ame­>)
if the current user has the exact role specified, false otherwise, regardless of '­adm­in' 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_form.sh­owR­ela­ted­Lis­t(<­rel­ate­dli­stt­abl­ena­me>)*
Hide Related List
g_form.hi­deR­ela­ted­Lis­t(<­rel­ate­dli­stt­abl­ena­me>)
Show All Related Lists
g_form.sh­owR­ela­ted­Lis­ts();
Hide All Related Lists
g_form.hi­deR­ela­ted­Lis­ts();

Sections based API

Show/Hide a Section
g_form.se­tSe­cti­onD­isp­lay­(<s­ect­ion­nam­e>,­tru­e/f­alse);
Get All Sections*
g_form.ge­tSe­cti­onN­ames();
Get All Sections**
g_form.ge­tSe­cti­ons();
Check whether section is Visible
g_form.is­Sec­tio­nVi­sib­le(­<se­cti­onn­ame­>);
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_form.ad­dDe­cor­ati­on(­<fl­d>,­ico­nna­me,­tit­lef­ori­con);
Remove Decora­tio­ns(not applicable for Catalog Client Scripts)
g_form.re­mov­eDe­cor­ati­on(­<fl­d>,­ico­nna­me,­tit­lef­ori­con);
LiveUpdate running on form*
g_form.is­Liv­eUp­dat­ing()
*Live update can detect any change of the data on the form from another session.

HTML based APIs

Control a Field
g_form.ge­tCo­ntr­ol(­<fl­dna­me>)
Get an HTML Element from Parameter*
g_form.ge­tEl­eme­nt(­<pa­ram­ete­r>)
Get HTML element of Option**
g_form.ge­tOp­tio­n(<­fie­ldn­ame­>,v­alue);
Get HTML element of Form
g_form.ge­tFo­rmE­lem­ent()
Get HTML element of HelpText
g_form.ge­tHe­lpT­ext­Con­tro­l(<­fld­nam­e>);
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_form.su­bmi­t(a­cti­onName)
Save a form
g_form.save()

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 : Instance 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.

Badges

Nice Cheat Sheet Good Cheat Sheet 🇱🇹 Big in Lithuania

Comments (1)

kea235 19:58 11 Oct 2020

Thanks for the cheatsheets

Reply

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.