fetch all fields in a g_form

georgechen
Kilo Guru

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>.
12 REPLIES 12

Vikas-Malhotra
Mega Guru

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


Madhu80
ServiceNow Employee

George, you could try using g_form.elements which would return you an array of the element objects.



Hope this helps.


brianmwatson
Tera Contributor

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]


        }


}


TypeError: g_form.getFieldNames is not a function