Server-side code should not use GlideRecord and getRowCount() to count records ??

GV Saranesh Kum
Kilo Guru

Hi All,

If there is a scenario, to know the row count and also fetch some values, then how will you be coding?

Per Best practice, will you do GlideAggregate to get the row count and glide record to fetch the values

or

Will you only use glide record to fetch values and use getRowCount to get count.

Let me know.

Thanks

Saranesh.

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Kumar,



Yes getRowCount() is not recommended, you can use GlideAggregate.


GlideAggregate



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Gurpreet07
Mega Sage

Hi Saranesh,



Yes, as per best practices , getRowCount is not recommended but if you are already having a GlideRecord statement to fetch some values then it would not be a wise to again use GlideAggregate. I have seen getRowCount in oob widgets but at the time its also fetching other values.


What I personally think is , getRowCount function at the backend again run a loop and increasing the count variable one by one and returning the same. So instead of running a while loop twice (first time when we fetch values and again when getRowCount called), prefer to decayer count(=0) variable before the while loop statement and increment the same by one in side the loop and fetch the values in same loop. below loop, you are having row count in count variable.


Hi Gurpreet,



Thanks for the reply. I agree with you.


However, i have scenarios were i need the row count for comparision before i go into while loop. I don't think the solution you articulated will work.