Return All Active Field Names and Values for a Particular Record

jmiskey
Kilo Sage

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);
            }
        }
    }

 

8 REPLIES 8

Sachin_Nasa
Tera Guru

Hi @jmiskey jmiskey,

 

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);
            }
        }
    }
}
P1.png

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.

Thanks & Regards,
Sachin Narayanasamy

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.

Yeah it seems I also noticed it, Facing diffuculties in attaching the image in the post

Thanks & Regards,
Sachin Narayanasamy

Sachin_Nasa
Tera Guru

Hi @jmiskey  jmiskey,

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.

P11.png

 

Thanks & Regards,
Sachin Narayanasamy