Field Count Per Table

J Huber
Kilo Contributor

Hi all.

I'm looping through tables to show the NAME, ROWCOUNT and FIELD-COUNT.

 

The count of fields part is puzzling me.

See the ????????.

 

var tableGR = new GlideRecord('sys_db_object');
tableGR.addQuery('name', 'STARTSWITH', 'u_');
tableGR.query();
while(tableGR.next()) {
	var curTableGR = new GlideRecord(tableGR.name.toString());
	curTableGR.query();
	
	var curdict = [];
	var fields = ??????????
	
	curdict.fields = fields.??????????
	curdict.name = tableGR.name.toString();
	curdict.count = curTableGR.getRowCount();
	
	gs.print('TableDetail: ' + curdict.name + ', ' + curdict.count + ', ' + curdict.fields);
}
1 ACCEPTED SOLUTION

perkinsarm
Mega Guru

I don't believe you can get that information directly.


For each table you will need to run a separate GlideAggregate search on [sys_dictionary] to get the count of the current table's fields.

For example, to get the number of Incident fields:

// Count all incident columns
var count = new GlideAggregate('sys_dictionary');
count.addAggregate('COUNT');
count.addQuery('name','incident');
count.addQuery('element','!=','');
count.query();
var incidents = 0;
if(count.next()) 
   incidents = count.getAggregate('COUNT');

gs.info(incidents);

View solution in original post

4 REPLIES 4

perkinsarm
Mega Guru

I don't believe you can get that information directly.


For each table you will need to run a separate GlideAggregate search on [sys_dictionary] to get the count of the current table's fields.

For example, to get the number of Incident fields:

// Count all incident columns
var count = new GlideAggregate('sys_dictionary');
count.addAggregate('COUNT');
count.addQuery('name','incident');
count.addQuery('element','!=','');
count.query();
var incidents = 0;
if(count.next()) 
   incidents = count.getAggregate('COUNT');

gs.info(incidents);

J Huber
Kilo Contributor

How about from getFields?

 

https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=r_GlideRecord-getFields

 

That sample has a .size()

 

Could that be used to count fields?

perkinsarm
Mega Guru

It looks like it could be used, but you will still have to load an actual record from each table to use getFields().

Also, if your table extends another table will you want to count the columns from the extended table too?

 

J Huber
Kilo Contributor

That example does it for me the!

Thanks for pointing out I'd have to load a record to use getFields().

 

getFields() wont work here since many of my tables don't have any records in them.