How to get all field names of a table?

anchor
Kilo Contributor

How to get all field names of a table through server script?

1 ACCEPTED SOLUTION

Sagar Patro
Kilo Guru
var arr_fields=[];
var fields = new GlideRecord('sys_dictionary');
fields.addQuery('name','incident');

fileds.addEncodedQuery('internal_type!=collection^ORinternal_type=NULL');//To ignore all the table dictionaries(optional)

fields.query();

while(fields.next())
{
arr_fields.push(fields.column_label.toString()+"("+fields.element.toString() + ")" );
}


for(var i=0; i<arr_fields.length; i++)
{
   gs.print(arr_fields[i]);
}

 

Please mark ot helpful and correct if it was.

Regards!

View solution in original post

9 REPLIES 9

Sagar Patro
Kilo Guru
var arr_fields=[];
var fields = new GlideRecord('sys_dictionary');
fields.addQuery('name','incident');

fileds.addEncodedQuery('internal_type!=collection^ORinternal_type=NULL');//To ignore all the table dictionaries(optional)

fields.query();

while(fields.next())
{
arr_fields.push(fields.column_label.toString()+"("+fields.element.toString() + ")" );
}


for(var i=0; i<arr_fields.length; i++)
{
   gs.print(arr_fields[i]);
}

 

Please mark ot helpful and correct if it was.

Regards!

Thanks for your great answer, it helps me a lot!

Hi,

 

What if table has extends from other table. In above case Incident has extends from Task and if you run mentioned script it will miss the fields which are from task table like description, short_description... etc

Is there anyway to get all fields from existing table and parent table as well.

 

You have a typo on line 5 ("fileds") but once that's fixed the script works beautifully. Thank you!!

Constantine Kr1
Giga Guru

The sys_dictionary table will not show inherited fields directly. (i.e. "short_description" from the task table if querying the incident table).

Use the following script to get all fields (including inherited fields) along with its Label, DB Name, and internal type. 

var tableName = "alm_hardware";

var grTableData = new GlideRecord(tableName);
grTableData.setLimit(1);
grTableData.query();
grTableData.next();

var fields = grTableData.getFields();

gs.info("Label, Field Name, Internal Type");
// Enumerate GlideElements from GlideRecord
for (var i = 0; i < fields.size(); i = i + 1) {
    var glideElement = fields.get(i);
    var label = glideElement.getLabel();
    var internalType = String(grTableData.getElement(glideElement.getName()).getED().getInternalType().toString());
    var database_field_name = glideElement.getName();
    gs.info("{0}, {1}, {2}", label, database_field_name, internalType);
}

//Sample Output
//*** Script: Label, Field Name, Internal Type
//*** Script: Parent, parent, reference
//*** Script: Short Description, short_description, string
//........