- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2020 11:50 AM
Hello,
I need a GlideRecord that checks if there is more than one record that matches conditions in a table. Here is what I have so far:
var check = true;
var selectTemplate = new GlideRecord('x_utsll_mass_maili_templates');
selectTemplate.addQuery('active', check);
selectTemplate.query();
if (selectTemplate.next()) {
}
How can I modify this to check if there is more than one record where the 'active' field is set to 'true'?
If there is more than one record that matches this condition I will display an error message to the user
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2020 11:59 AM
Hi JJG,
You could perform a GlideAggregate query and return a count. Would that help? Then you can just check if the count is greater than 1 or something, and display your error message.
Is this also Client Side? Server Side? onChange? onSubmit?
Server side for example:
(function() {
var ga = new GlideAggregate('table_name');
ga.addQuery('field_name', 'field_value');
ga.addAggregate('COUNT');
ga._query();
if(ga._next()) {
if(ga.getAggregate('COUNT') > 1) {
// your error goes here
}
}
})();
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2020 11:53 AM
Hi,
You need to use 'while' instead of 'if'. So, something like while(selectTemplate.next())
& then in the { pass your comments }.
Something like below.
while(selectTemplate.next()) {
//pass your error message }

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2020 11:59 AM
Hi JJG,
You could perform a GlideAggregate query and return a count. Would that help? Then you can just check if the count is greater than 1 or something, and display your error message.
Is this also Client Side? Server Side? onChange? onSubmit?
Server side for example:
(function() {
var ga = new GlideAggregate('table_name');
ga.addQuery('field_name', 'field_value');
ga.addAggregate('COUNT');
ga._query();
if(ga._next()) {
if(ga.getAggregate('COUNT') > 1) {
// your error goes here
}
}
})();
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2020 12:03 PM
Agree with Mark, anytime you need to count records it is much more efficient to use GlideAggregate.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hello, it does not seem that obvious to me for this case.
Here the need is not to know the actual number of records, but just to know if there is more than one.
I believe when we want to know if there is "at least one" record, using a GlideRecord with setLimit( 1 ) is an efficient solution (may be more than the GlideAggregate ?).
So I was wondering for the case "more than one", how a solution based on GlideRecord with setLimit( 2 ) would compare to the GlideAgregate.