Help to replace getRowCount() in script.

Meenal Gharat
Giga Guru

Hello all,

In the below script I am trying replace getRowCount.

Running on Before Insert Business rule. Can we replace gr.getRowCount() with if (gr.hasNext())?

Or can this be replaced with GlideAggreate as I am not sure how this getRecordClassName() function will work with GlideAggregate.

var curNum = current.number + '';

if(curNum) {
var recordClass = current.getRecordClassName();
var gr = new GlideRecord(recordClass);
gr.addQuery('number', curNum);
gr.query();

if(gr.getRowCount() > 0) {
var newNum = getNextObjNumberPadded();
current.number = newNum;
}
}

 

Thank you!

Regards,

Meenal

 

1 ACCEPTED SOLUTION

MrMuhammad
Giga Sage

You can replace with 

if(gr.hasNext()) 

OR

if(gr.next())

 

Simple definition

hasNext() --> check whether any record found against the query

next() --> if record found then ready the record to perform an action

 

 

Regards,
Muhammad

View solution in original post

3 REPLIES 3

MrMuhammad
Giga Sage

You can replace with 

if(gr.hasNext()) 

OR

if(gr.next())

 

Simple definition

hasNext() --> check whether any record found against the query

next() --> if record found then ready the record to perform an action

 

 

Regards,
Muhammad

sachin_namjoshi
Kilo Patron
Kilo Patron

use below code

 

var curNum = current.number + '';

if(curNum) {
var recordClass = current.getRecordClassName();
var gr = new GlideRecord(recordClass);
gr.addQuery('number', curNum);
gr.query();

if(gr.hasNext()) {
var newNum = getNextObjNumberPadded();
current.number = newNum;
}
}

 

Regards,

Sachin

MrMuhammad
Giga Sage

Regarding the GlideAggregate, you don't need that here as you are not concerned about the total count of records instead you just need to check whether or not the record returned against the applied query. So hasNext() is the best alternative in this scenario.

Regards,
Muhammad