- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 12-28-2019 11:13 AM
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.<method name>
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.
- Example:
function onLoad() {
var callerID = g_form.getDisplayBox('caller_id').value;
g_form.addInfoMessage(callerID);
}
- Note:
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
- 13,458 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
What is g_form.resolveNameMap() ?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
The g_form.resolveNameMap function compares the name of a variable against g_form's nameMap property. nameMap have following information available about the catalog item's variables:
"prettyName": "requested_by",
"realName": "IO:58f54f302f8a4010dabd59a72799b63f",
"label": "Requested By",
"questionID": "58f54f302f8a4010dabd59a72799b63f"
Thanks,
Abhishek Gardade
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
HI Abhishek,
These APIs fail to work in Agent workspace. can you please verify and provide if there is any alternative for g_form.mandatoryCheck()
Thanks
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Okay, I will try to find provide you altenative. May I know the Requirement?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi @AbhishekGardade
g_form.getEditableFields() does not work for agent workspace. Can you suggest some solution to make fields read only in agent workspace using client script?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Isn't workspace considered portal? Mobile/Service Portal UI Type
-- these seem very specific to be used when setting up scripts. Chrome > dev tool > 3 dots > more tools > search then type g_form. you can scroll through all the actively used g_forms on the current page. There's probably an easier way to find these.