Useful GlideForm Client-side API Methods
ServiceNow provides client-side JavaScript APIs allowing you to control aspects of how ServiceNow is displayed and functions within the web browser.
The GlideForm client-side API provides methods for managing form and form fields including methods to:
- Retrieve a field value on a form
- Making a field read-only, hidden and mandatory
- Writing a message on a form or a field
- Adding & Removing fields from a choice list
There is no constructor for the GlideForm class. The GlideForm methods are accessed through the global g_form object that is only available in client-side scripts.
We can access these methods of the GlideForm class using the g_form global object.
g_form.
In the ServiceNow docs, you will get a list of all the methods available for GlideForm (g_form) API.
Available methods for GlideForm API
Though there are some methods which are not available in the documentation and sometimes we require such methods which are very useful.
So, Lets understand their functionality and use case..
# g_form.mandatoryCheck();
- This method returns Boolean value and doesn’t have any parameter.
- What it Returns: Returns false if any of mandatory fields are not filled with value: otherwise returns true*.*
- Where to use: You can write this code at any client side scripts like UI Policies, client scripts or catalog client scripts/ UI Policies.
- Limitations: This method will not work on Service Portal but will work on Native UI for catalog item.
- Example: Check out this code:
function onSubmit() {
var isMandatory = g_form.mandatoryCheck();
alert(isMandatory);
}
As you can see in image, isMandatory will return false as few fields are not filled and in errorMessage it will give you the name of the fields which are not filled and mandatory.
# g_form.getMissingFields():
- This method returns array of comma separated names of all the fields that are mandatory and are not filled.
- Usually this method wouldn't be much use to you, because submit () function will call this method to check if all the mandatory fields are filled or not.
- What it Returns: Returns array of comma separated names of all the fields that are mandatory otherwise an empty array If all the mandatory fields are filled*.*
- Where to use: You can write this code at any client-side scripts like UI Policies or client scripts
- Limitations: This method will not work on Service Portal but will work on Native UI for catalog item.
- Example:
This method will be a lifesaver, if you are working for an idea like "draft" in Service Catalog, where you need to make all the mandatory fields on the form non-mandatory, but still track the mandatory fields that weren't filled when the "draft" button is clicked(You need to call this method in draft button, before you make all mandatory fields non mandatory, and hence get all the mandatory fields which are not filled)
function onSubmit() {
//Type appropriate comment here, and begin script below
var arrayOfFields = g_form.getMissingFields();
g_form.addInfoMessage("The names of fields which are mandatory but not filled : " + arrayOfFields);
}
#g_form.getEditableFields()
- This method returns array of comma separated names of all the fields which are not read-only.
- What it Returns: Returns array of comma separated names of all the fields that are editable or not read only otherwise an empty array If all fields are non-editable or read only*.*
- Where to use: You can write this code at any client-side scripts like UI Policies or client scripts
- Limitations: This method will not work on Service Portal but will work on Native UI for catalog item.
- Example:
This method will save your time and line of code. Here I am using it for below use cases. g_form.elements will fetch all fields in a g_form and getEditableFields method will only fetch editable fields.
1.Make complete form Read only
function onLoad() {
var fields = g_form.getEditableFields();
for (var x = 0; x < fields.length; x++) {
var field = g_form.getValue(fields[x]);
g_form.setReadOnly(fields[x], true);
}
}
2. Make all fields Mandatory
function onLoad() {
var fields = g_form.getEditableFields();
for (var x = 0; x < fields.length; x++) {
var field = g_form.getValue(fields[x]);
g_form.setMandatory(fields[x],true);
}
}
3.Make all fields Hidden
function onLoad() {
var fields = g_form.getEditableFields();
for (var x = 0; x < fields.length; x++) {
var field = g_form.getValue(fields[x]);
g_form.setDisplay(fields[x],false);
}
}
Apart from this, same thing can be done by g_form.elements as below
for (var i = 0; i < g_form.elements.length; i++) {
var el = g_form.elements[i];
var fieldName = el.fieldName;
g_form.setMandatory(fieldName, true);
}
#g_form.getDisplayBox(‘field_name’).value
- Returns a Display Value of a Reference field that was on the form*.*
- So, running AJAX server-side call to the database to retrieve the the Display Value of a Reference field. This invokes server-side logic that is unnecessary when the browser is storing this on the client. You can use the following in your client script to retrieve the display value instead of the usual sys_id that is retrieved:
- Where to use: You can write this code at any client-side scripts like UI Policies or client scripts.
- Limitations:
- This method will not work on Service Portal but will work on Native UI for catalog item.
- This method is not supported in Mobile View.
- Does not work when the field is read only by an ACL.
function onLoad() {
var callerID = g_form.getDisplayBox('caller_id').value;
g_form.addInfoMessage(callerID);
}
In the Service Catalog, you may have use this if above method is not working.
var userName = g_form.getDisplayBox(g_form.resolveNameMap("opened_by")).value;
alert("userName: "+userName);
As their is no documentation available for this, it doesn't mean that these methods are deprecated . So do comment with your suggestion and additional points if you are aware of such methods.
Hope you will find it helpful as well. Don’t forget to Mark it Helpful and Bookmark. Thanks,
Abhishek Gardade
https://www.servicenow.com/community/developer-articles/useful-glideform-client-side-api-methods/ta-p/2321721