How to get row count and print number of records in a glide query

vahini
Giga Guru

Posting this code thinking it might be usefull to anyone: i have 5 incidents satisfying the below query

 

I wanted to write a back script to get number of records satisfying  below query

 

var inc = new GlideRecord("incident");

 

inc.addEncodedQuery("active=true^incident_stateNOT IN6,7^short_descriptionSTARTSWITHInvalid Assigned To^u_requested_for=879a48fc0a0a3caa01b7dc8eba6a7593^incident_state=1^state=1");

 

inc.query();

 

gs.print(inc.getRowCount());

 

output  : 5

*************************************************************

 If you want to print record numbers of each incdient in the result then use below script:

 

var inc = new GlideRecord("incident");

 

inc.addEncodedQuery("active=true^incident_stateNOT IN6,7^short_descriptionSTARTSWITHInvalid Assigned To^u_requested_for=879a48fc0a0a3caa01b7dc8eba6a7593^incident_state=1^state=1");

 

inc.query();

while(inc.next())
{
gs.print(inc.number);
}

 

out put : prints all 5 numbers of incidents

 

***************************************************************

 

If you want to print number of records and also each record number then use below code

var inc = new GlideRecord("incident");

 

inc.addEncodedQuery("active=true^incident_stateNOT IN6,7^short_descriptionSTARTSWITHInvalid Assigned To^u_requested_for=879a48fc0a0a3caa01b7dc8eba6a7593^incident_state=1^state=1");

 

inc.query();

gs.print(inc.getRowCount());

while(inc.next())
{
gs.print(inc.number);
}

 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi Vahini,

 

Please do not use GlideRecord for fetching counts as currently you might be working on small database but on larger scale you should use GlideAggregate() 

 

For Example:

 

 

        var gaIn = new GlideAggregate('incident');
        gaIn.addEncodedQuery("active=true^incident_stateNOT IN6,7^short_descriptionSTARTSWITHInvalid Assigned To^u_requested_for=879a48fc0a0a3caa01b7dc8eba6a7593^incident_state=1^state=1");
        gaIn.addAggregate('COUNT');
        gaIn.query();
        while(gaIn.next()){
            gs.addInfoMessage(gaIn.getAggregate('COUNT'));
        } 

 

 

If you found my answer useful, please mark as correct answer.

 

Thanks,

Kaustubh Kulkarni

View solution in original post

1 REPLY 1

Community Alums
Not applicable

Hi Vahini,

 

Please do not use GlideRecord for fetching counts as currently you might be working on small database but on larger scale you should use GlideAggregate() 

 

For Example:

 

 

        var gaIn = new GlideAggregate('incident');
        gaIn.addEncodedQuery("active=true^incident_stateNOT IN6,7^short_descriptionSTARTSWITHInvalid Assigned To^u_requested_for=879a48fc0a0a3caa01b7dc8eba6a7593^incident_state=1^state=1");
        gaIn.addAggregate('COUNT');
        gaIn.query();
        while(gaIn.next()){
            gs.addInfoMessage(gaIn.getAggregate('COUNT'));
        } 

 

 

If you found my answer useful, please mark as correct answer.

 

Thanks,

Kaustubh Kulkarni