getrowcount for GlideRecord

ceraulo
Mega Guru

Hello!

The objective of my script below is to get the Description field of the SCTASK from the RITM table. If there are more than one SCTASK for the RITM, it would get the value of the ITEM field.

var gr = new GlideRecord('sc_task');
gr.addQuery('request_item', current.sys_id);
gr.query();

var count = gr.getRowCount();
var desc;

if (count == '1')
desc = gr.description;
else
desc = current.cat_item;

Please help!

Thank you.

1 ACCEPTED SOLUTION

I was able to make this work with this script:

 

var count = 0;
var desc;

var gr = new GlideRecord('sc_task');
gr.addQuery('request_item', current.sys_id);
gr.query();

while (gr.next()) {
count = count + 1;
}

if (count == 1) {
desc = gr.description;
}
else {
desc = current.cat_item.getDisplayValue();
}

 

May I also know why getRowCount is not recommended?

View solution in original post

5 REPLIES 5

Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi,

We dont use getRowCount it is not recommended.


You can simple do like this:

var gr = new GlideRecord('sc_task');
gr.addQuery('request_item', current.sys_id);
gr.query();
var desc;

if (gr.next())
desc = gr.description;
else
desc = current.cat_item;

 

OR 

 

Use GlideAggregate.


Thanks,
Ashutosh

I was able to make this work with this script:

 

var count = 0;
var desc;

var gr = new GlideRecord('sc_task');
gr.addQuery('request_item', current.sys_id);
gr.query();

while (gr.next()) {
count = count + 1;
}

if (count == 1) {
desc = gr.description;
}
else {
desc = current.cat_item.getDisplayValue();
}

 

May I also know why getRowCount is not recommended?

Hi,

sharing link which is for best practice around glideaggregrate and getrowcount

https://community.servicenow.com/community?id=community_question&sys_id=6a16d0e9db59f38813b5fb243996...

https://mavembry.info/post/gr-and-ga/

https://community.servicenow.com/community?id=community_question&sys_id=47c1c3a9db98dbc01dcaf3231f96...

Regards
Ankur

 

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

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

Script looks fine. Seems  you are using it as a Business rule script that runs on RITM table after insert/update.

Try replacing
if (count == '1')

with
if (count >=1)