Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

if statement with empty query

Brad Tilton
ServiceNow Employee

I don't have a strong javascript background and was wondering if someone could answer this question. I want to write an if statement that does something based on whether my query returned anything or not.


var gr = new GlideRecord('sc_cat_item');
gr.addQuery('u_software', current.sys_id);
gr.query();

if (gr didn't return a record)
dothis();
else
dothat();


How do I need to write the if statement or add to the query?

Thanks,
Brad
2 REPLIES 2

Not applicable

Try:


if (gr.hasNext()) {
dothis(); // software found
} else {
dothat(); // didn't find it
}


You can also try gr.getRowCount() with similar results, or while (gr.next()) for multiple.

http://wiki.service-now.com/index.php?title=GlideRecord#hasNext


Brad Tilton
ServiceNow Employee

That works great, thanks!