- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-14-2019 01:57 PM
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);
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-14-2019 02:42 PM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-14-2019 02:42 PM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-15-2019 06:20 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-15-2019 07:12 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-15-2019 12:15 PM
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.