How can I get the field names in a form?

drberg
Giga Contributor

I'm trying to create a client script that will get all the names of all fields in for example the Incident table.

For example

When I go to create a new Incident the jslog will print the names of the fields.

Pseudo-ish code:

var fields = get names of fields;

for(i = 0; i < fields.length; i++) {

        jslog("Field " + i + ": " + fields[i]);

}

Result:

Field 0: number

Field 1: caller_id

Field 2: location

...

The reason I'm asking is because I'm trying to create an easter egg. I will of course post an article back in the community when the easter egg is done!

5 REPLIES 5

Abhinandan Pati
Giga Guru

Hi Magnus,



On the client side you can use below statement



g_form.getLabel('<field_name>');



Whereas for server side scripting you can use 'getElement' method.



Thanks,


Abhinandan


Mihir Mohanta
Kilo Sage

Hi Magnus,



Create a onload client script like below :



function onLoad() {


    //Type appropriate comment here, and begin script below


var fieldslist = '';


  var count = 0 ;


var fields = new GlideRecord('sys_dictionary');


  fields.addQuery('name','incident'); // Write the table name for which you need field name list


  fields.query();


  while(fields.next()){


  if(count > 0)


  fieldslist = fieldslist +","+ fields.element ;


  if(count == 0){


  fieldslist = fields.element;


  count += 1 ;


  }



  }


  alert("Field list of the incident table is : " +fieldslist )


}




Thanks,


Mihir


Thanks! That's almost it. It doesn't list the fields from the parent tables.


if you are looking for just the parent task table then you will have to list it in the query. so just adding on to what Mihir stated change:



fields.addQuery('name','incident'); // Write the table name for which you need field name list



to



fields.addEncodedQuery('name=task^ORname=incident');



this will grab both table fields.