fetch all fields in a g_form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2016 07:37 PM
Hi folks again,
Is there a way to loop through available fields in a g_form, instead of hard-coding each field name. g_form has a method getControl which expects a field name, the control can be fetched, but you have to specify the fieldname, I wonder if the fields can be dynamically lopped through the g_form fields (or variable something like this)
Any advice would be appreciated.
Thanks,
GlideForm (g form) - ServiceNow Wiki
10.2 getControl
HTMLElement getControl(fieldName)
- Returns the HTML element for the specified field. Compound fields may contain several HTML elements.
- Generally not necessary as there are built-in methods that use the fields on the form.
- Parameters:
- fieldName - specifies the name of the field.
- Returns:
- HTMLElement - the specified HTML element(s).
- Note: If the field is of type reference and the control is a choice list, getControl() may not return control as expected. In this case, use sys_select.<table name>.<field name>.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2017 06:37 PM
I previously tried to do this and had some success using the 'nameMap' object. Here are a couple of snippets that may be of help. The second one was used to loop through all fields and toggle the help annotation on:
for (var index = 0; index < g_form.nameMap.length; index++) {
jslog("Name Map # " + index + ": " + g_form.nameMap[index].prettyName);
}
for (var index = 0; index < g_form.nameMap.length; index++) {
jslog("Name Map # " + index + ": " + g_form.nameMap[index].prettyName);
var helptext = g_form.getControl(g_form.nameMap[index].prettyName);
toggleHelp(helptext.id);
}
Not sure if that is useful.
Cheers,
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2017 05:47 AM
Hi George,
g_form.nameMap works only for variables. For fields, you can use g_form.elements, as what @Madhu Umapathy said.
I use something like this:
for(var x = 0; x < g_form.elements.length; x++){
g_form.setDisplay(g_form.elements[x].fieldName, true);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2019 01:26 AM
I tried below script and it makes the variables along with fields on catalog task readonly except for the variables which are reference/checkbox.
function onLoad() {
if(g_form.getValue('active') == "false"){
for(var x = 0; x < g_form.elements.length; x++)
{
g_form.setReadOnly(g_form.elements[x].fieldName, true);
}
}
}
Is there a way i can make only fields read only not the variables on catalog task? (Instance is Madrid)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2020 03:31 AM
This was my solution, using only documented commands...
var dict = new GlideRecord("sys_dictionary");
dict.addQuery("name", g_form.getTableName());
dict.addQuery("internal_type", "!=", "collection");
dict.query();
while (dict.next()) {
var elem = dict.getValue("element");
if (g_form.getElement(elem)) {
// show only fields that exist on this form...
g_form.addInfoMessage(elem);
}
}
The g_form.addInfoMessage(elem); can be changed to whatever!
Regards, Neil

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2020 03:36 AM
writing glide record in client script is not good practice.
instead of writing the glide record part in client script , use glide ajax here .
create one client callable script include and use the glide record part there and then use glide ajax inside the client script to get the all details in g_form.addInfoMessage()