Return All Active Field Names and Values for a Particular Record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2026 12:00 PM
I have the following code which correctly returns the field names and values for every non-system field in a table, for a particular record. I would like to add one more criteria: I would like to exclude all non-Active fields in the table from the listing it returns. How do I amend this code to do that?
var incNumber = 'INC0795071'; //enter specific Incident number
var target = new GlideRecord('incident'); // Replace 'incident' with your table name
target.addQuery('number', incNumber); // Query for a specific record
target.query();
var gru = new GlideRecordUtil();
if (target.next()) {
// Get all field names for the current record
var fieldNames = gru.getFields(target);
for (var i = 0; i < fieldNames.length; i++) {
var fieldName = fieldNames[i];
var fieldValue = target.getValue(fieldName);
//exclude all system variables
if(fieldName.substr(0,4) != 'sys_'){
gs.print(fieldName + ": " + fieldValue);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2026 12:24 PM
You can’t filter inactive fields directly from the table GlideRecord because field activity is stored in the sys_dictionary.
To exclude inactive fields, query sys_dictionary for the table and element and use addActiveQuery() before printing the field value.
🔹 Part to add inside your loop
var dict = new GlideRecord('sys_dictionary');
dict.addQuery('name', target.getTableName());
dict.addQuery('element', fieldName);
dict.addActiveQuery(); // only active fields
dict.query();
if (dict.next()) {
gs.print(fieldName + ": " + fieldValue);
}
The full code is as follows:
Hope this helps!
If this resolves your issue, please mark the response as Helpful and Accept it as the Solution so it can assist others as well.
Sachin Narayanasamy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2026 12:43 PM
Hmmm, so that does somewhere weird when I run it in a background script. I added a counter to see how many records it would return. It should be about 142. It returns a count of 79. I tried it on another table that has 143 records that should be returned, and it only returns 80.
So it seems to be excluding a lot more records than it should.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2026 02:06 PM
Yeah it seems I also noticed it, Facing diffuculties in attaching the image in the post
Sachin Narayanasamy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2026 02:02 PM
You can’t filter inactive fields directly from the table GlideRecord because field activity is stored in the sys_dictionary.
To exclude inactive fields, query sys_dictionary for the table and element and use addActiveQuery() before printing the field value.
Part to add inside your loop
var dict = new GlideRecord('sys_dictionary');
dict.addQuery('name', target.getTableName());
dict.addQuery('element', fieldName);
dict.addActiveQuery(); // only active fields
dict.query();
if (dict.next()) {
gs.print(fieldName + ": " + fieldValue);
}
The full code is as follows:
var incNumber = 'INC0009009';
var target = new GlideRecord('incident');
target.addQuery('number', incNumber);
target.query();
var gru = new GlideRecordUtil();
if (target.next()) {
var fieldNames = gru.getFields(target);
for (var i = 0; i < fieldNames.length; i++) {
var fieldName = fieldNames[i];
var fieldValue = target.getValue(fieldName);
// exclude system fields
if (fieldName.substr(0, 4) != 'sys_') {
// ---- NEW PART: check field active in dictionary ----
var dict = new GlideRecord('sys_dictionary');
dict.addQuery('name', target.getTableName());
dict.addQuery('element', fieldName);
dict.addActiveQuery(); // active = true
dict.query();
if (dict.next()) {
gs.print(fieldName + ": " + fieldValue);
}
}
}
}
Hope this helps!
If this resolves your issue, please mark the response as Helpful and Accept it as the Solution so it can assist others as well.
Sachin Narayanasamy
