Service-Now Client Script APIs
Field-Based APIs
Hide/Display variable : g_form.setDisplay(<fld>,true/false);
Disable a field : g_form.setDisable(<fld>,true/false);
Field ReadOnly : g_form.setReadOnly(<fld>,true/false);
Clear Value from Field : g_form.clearValue(<fld>);
Change Label of Field : g_form.setLabelOf(<fld>,newlabel);
Make field mandatory : g_form.setMandatory(<fld>,true/false);
Set Value of Field : g_form.setValue(<fld>,value);
Show error and scroll : g_form.showErrorBox(<fld>,msg,true/false);
to field
Add Options to Choice : g_form.addOption(<fld>,value,label,index);
filed
Remove Options from : g_form.removeOption(<fld>,value);
Choice field
Clear all Options from : g_form.clearOptions(<fld>);
Choice Field
Get Reference field : g_form.getReference(<fld>,callbackfunction);
Value and Attributes
Flash a particular field : g_form.flash(table.name,color,no of flashes)
Get Integer Value of Field: g_form.getIntValue(<fieldname>);
Get Decimal Value of Field: g_form.getDecimalValue(<fieldname>);
**Note:
<fld> 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_form.showRelatedList(<relatedlisttablename>);
Hide Related List : g_form.hideRelatedList(<relatedlisttablename>);
Show All Related Lists : g_form.showRelatedLists();
Hide All Related Lists : g_form.hideRelatedLists();
Sections based API
Show/Hide a Section : g_form.setSectionDisplay(<sectionname>,true/false);
Get All Sections* : g_form.getSectionNames();
Get All Sections** : g_form.getSections();
Check whether section : g_form.isSectionVisible(<sectionname>);
is Visible
Note:
*Returns all the sections including the invisible ones,
**Not available on Mobile Platform
<sectionname>: 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 "section_specificapi".
Decorations API
Add decorations(not applicable : g_form.addDecoration(<fld>,iconname,titleforicon);
for Catalog Client Scripts)
Remove Decorations(not applicable : g_form.removeDecoration(<fld>,iconname,titleforicon);
for Catalog Client Scripts)
LiveUpdate running on form* : g_form.isLiveUpdating()
Note:
*Live update can detect any change of the data on the form from another session.
HTML based APIs
Control a Field : g_form.getControl(<fldname>);
Get an HTML Element from Parameter* : g_form.getElement(<parameter>);
Get HTML element of Option** : g_form.getOption(<fieldname>,value);
Get HTML element of Form : g_form.getFormElement();
Get HTML element of HelpText : g_form.getHelpTextControl(<fldname>);
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_form.submit(actionName);
Save a form : g_form.save();
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 GlideRecord function in Client script: Replace the calls by GlideAjax.
Avoid DOM(manipulation of form elements): As all the elements might be changed in the future release,
and to avoid complications avoid doing DOM.
Use g_scratchpad 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: Instance before London, has no possibility 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.
https://www.servicenow.com/community/developer-articles/service-now-client-script-apis/ta-p/2322509