if statement with empty query

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2009 08:03 AM
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
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2009 04:55 PM
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

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2009 07:32 AM
That works great, thanks!