Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to use GlideAggregate

yoli1
Tera Contributor

Hi All how can i use Glide Aggregate rather than using .getRowCount() in my code? Thanks.

 

 

var userGr = new GlideRecord("sys_user");
userGr.addEncodedQuery("active=true^u_country=4538b7111b121100763d91eebc0713f1");	
userGr.query();
if(userGr.hasNext() && userGr.getRowCount() > 500) {
return;
}
while(userGr.next()){
userGr.setValue("active", false);
userGr.update();
}

 

 

 

5 REPLIES 5

Deepak Shaerma
Kilo Sage
Kilo Sage

Hi @yoli1 

try this to fulfill your query.

var agg = new GlideAggregate(‘sys_user’);

// Add your query conditions
agg.addEncodedQuery(“active=true^u_country=4538b7111b121100763d91eebc0713f1”);

// Specify that you want to count the records
agg.addAggregate(‘COUNT’);

// Query the database
agg.query();

// Variable to hold the count
var count = 0;

// Retrieve the count result. There should be only one row returned by a count aggregate query
if (agg.next()) {
    count = agg.getAggregate('COUNT');
}

// Check if the count is greater than 500
if (parseInt(count) > 500) {
    return; // Exit the script or function
}

// Proceed with making users inactive if count is 500 or less
var userGr = new GlideRecord('sys_user');
userGr.addEncodedQuery("active=true^u_country=4538b7111b121100763d91eebc0713f1");
userGr.query();
while (userGr.next()) {
    userGr.setValue('active', false);
    userGr.update();
}

Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot.
Thanks & Regards 
Deepak Sharma