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
04-05-2016 09:10 PM
Hi George,
I don't think there is any way to loop through the fields of g_form like this but, you may try this instead of using g_form.
var g = new GlideRecord('incident')
g.get(g_form.getUniqueValue())
for(var gg in g)
{
g_form.addInfoMessage(gg)
}
You can get all the fields of incident form in this way.
Hope it is helpful.
Thanks and Regards,
Vikas Malhotra
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2016 09:11 PM
George, you could try using g_form.elements which would return you an array of the element objects.
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2017 05:10 PM
Have you tried the getFieldNames() method of g_form? Here's a example from a widget client script:
function doSomething() {
var allFields = g_form.getFieldNames();
for(var fieldName in allFields){
//do something with allFields[fieldName]
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2022 08:00 AM
TypeError: g_form.getFieldNames is not a function