Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to display selected values from an array of objects

Souful Isha
Tera Expert

I have written a customized Util class in script include which has a function to get records depending on a filter which is passed as parameters to a function.

var MyCustomUtils = Class.create();
MyCustomUtils.prototype = {
    initialize: function() {
    },
 
//function to get records depending on a filter
getRecordsByFilter: function(tableName, order, limit, filterField, filterValue){
       
        var tblName = tableName || 'incident';
        var ord = order || 'sys_created_on';
        var lmt = limit || 5;
           
        var records = [];
       
        var gr = new GlideRecord(tblName);  
        gr.setLimit(lmt);
        gr.orderByDesc(ord);
        gr.addQuery(filterField,filterValue);
        gr.query();
       
        while(gr.next()){
           
            var obj = {};
             for (var key in gr) {

              //copy all the fields

               obj [key] = gr[key];

            records.push(obj);          
        }
       
        return records;
       
    }

    type: 'MyCustomUtils'
}
};

 

The resultant records are stored in arrays as objects. I want to display certain selected fields from the entire record for each array element.

For eg:- I want to display the incident numbers, state, and its value from the object for each array element.

How can I achieve this? 

2 REPLIES 2

Community Alums
Not applicable
Hi, if i understood your question correctly you want to read from obj. If so, you can easily so by applying a loop on object and inside loop use obj['field name'], likewise you can read what is inside object for each index.

Souful Isha
Tera Expert

Thank you Abhay Kumar. It was simple yet I was unable to figure it out. Thank you.