How to return count of records

Supriya25
Tera Guru

Hi All,

 

Please help me how return count of records

count = 5;

getdata:function(count,query){

 

var List=[];

var gr=new GlideRecord('incident);

gr.addQuery(query);

gr.setLimit(1); // every time get one record only 

gr.query();

while(gr.next()){

List.push(gr.getValue('sys_id');

}

if(count>=List.length){



if List .length is less than count then again above code should execute  and in query time exclude List array records and get new records and add to old list records 


if List .length is == count then return that 5 records details as a final result.

1 ACCEPTED SOLUTION

Deepak Shaerma
Kilo Sage

Hi @Supriya25 

 

write a function that iteratively queries the “incident” table and excludes already fetched records until you reach the desired count. This can be done by using the IN operator in GlideRecord queries to exclude sys_ids present in the List.

function getdata(count, query) {

    var List = [];

    var gr, sys_id_list;

 

    while (List.length < count) {

        gr = new GlideRecord('incident');

        gr.addEncodedQuery(query); // Add the original query conditions

 

        // Exclude already fetched records

        if (list.length > 0) {

            sys_id_list = List.join(',');

            gr.addEncodedQuery('sys_idNOT IN' + sys_id_list); 

        }

gr.setLimit(1); // Normally, this would be dynamic, but to respect the given instruction.

        gr.query();

 

        var has_next = false;

        while (gr.next()) {

            List.push(gr.getValue('sys_id'));

            has_next = true;

        }

 

        // If gr.query() does not return any more records and List is still less than count, break the loop to avoid infinite loop

        if(!has_next){

            break;

        }

    }

return List;

}

 

// Example usage

var count = 5;

var query = ‘active=true’; // Example query

var result = getdata(count, query);

gs.info(‘Fetched sys_ids: ’ + result.join(’, '));

 

 

 

View solution in original post

4 REPLIES 4

Bert_c1
Kilo Patron

Your list will only have one element per your code (gr.setLimit(1);).  If you want to get a count of records from your query, see example:

 

var inc = new GlideRecord('incident');
inc.query();
gs.info ("Found " + inc.getRowCount() + " records");
while (inc.next()) {
	gs.info(inc.number);
}

 

Seems you need to add more details on what you want to do.

Deepak Shaerma
Kilo Sage

Hi @Supriya25 

 

write a function that iteratively queries the “incident” table and excludes already fetched records until you reach the desired count. This can be done by using the IN operator in GlideRecord queries to exclude sys_ids present in the List.

function getdata(count, query) {

    var List = [];

    var gr, sys_id_list;

 

    while (List.length < count) {

        gr = new GlideRecord('incident');

        gr.addEncodedQuery(query); // Add the original query conditions

 

        // Exclude already fetched records

        if (list.length > 0) {

            sys_id_list = List.join(',');

            gr.addEncodedQuery('sys_idNOT IN' + sys_id_list); 

        }

gr.setLimit(1); // Normally, this would be dynamic, but to respect the given instruction.

        gr.query();

 

        var has_next = false;

        while (gr.next()) {

            List.push(gr.getValue('sys_id'));

            has_next = true;

        }

 

        // If gr.query() does not return any more records and List is still less than count, break the loop to avoid infinite loop

        if(!has_next){

            break;

        }

    }

return List;

}

 

// Example usage

var count = 5;

var query = ‘active=true’; // Example query

var result = getdata(count, query);

gs.info(‘Fetched sys_ids: ’ + result.join(’, '));

 

 

 

Hi Deepak,

Yes, this is what I'm looking for, Let me Try your solution.

JohnnySnow
Kilo Sage

Hi @Supriya25 

 

What are you trying to achieve here? there could be easier ways to doing it.

gr.getRowCount() will show the total number of records your Glide query has brought in.

 

 

 

Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.