Check if there is MORE THAN ONE record that matches conditions in a table

JJG
Kilo Guru

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

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

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

LinkedIn

View solution in original post

3 REPLIES 3

Jaspal Singh
Mega Patron
Mega Patron

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 Roethof
Tera Patron
Tera Patron

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

LinkedIn

Agree with Mark, anytime you need to count records it is much more efficient to use GlideAggregate.