- 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.