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

Amit Pandey
Kilo Sage

Hi @yoli1 

 

Please check this-

 

var userGr = new GlideAggregate("sys_user");
userGr.addQuery("active", true);
userGr.addQuery("u_country", "4538b7111b121100763d91eebc0713f1");
userGr.addAggregate("COUNT");
userGr.query();
if (userGr.next()) {
    var userCount = userGr.getAggregate("COUNT");
    if (userCount > 500) {
        return; // Exit the script if count exceeds 500
    }
}

var userUpdateGr = new GlideRecord("sys_user");
userUpdateGr.addQuery("active", true);
userUpdateGr.addQuery("u_country", "4538b7111b121100763d91eebc0713f1");
userUpdateGr.query();
while (userUpdateGr.next()) {
    userUpdateGr.active = false;
    userUpdateGr.update();
}

Please mark my answer helpful and correct.

 

Regards,

Amit

Hi @Amit Pandey Thank you. what about if you want to update a record found in the GlideAggregate for example :

var userGr = new GlideAggregate("sys_user");
userGr.addQuery("active", true);
userGr.addQuery("u_country", "4538b7111b121100763d91eebc0713f1");
userGr.addAggregate("COUNT");
userGr.query();
if (userGr.getAggregate('COUNT') > 100) {
    while (userGr.next()) {
        //make user inactive
        //do we do it this way
        userGr.active = false;
        userGr.update();
        //or should we call a new glide record?

    }
}

 

Hi @yoli1 

 

In my understanding, GlideAggregate  is used to determine if the condition is met (In your case, if there are more than 500 users). Then, use GlideRecord to actually update the records if the condition is met. You can try this-

 

 

// Step 1: Use GlideAggregate to count records
var agg = new GlideAggregate('sys_user');
agg.addQuery("active", true);
agg.addQuery("u_country", "4538b7111b121100763d91eebc0713f1");
agg.addAggregate("COUNT");
agg.query();

if (agg.next() && agg.getAggregate('COUNT') > 100) {
    // Step 2: Use GlideRecord to fetch and update the records
    var userGr = new GlideRecord('sys_user');
    userGr.addQuery("active", true);
    userGr.addQuery("u_country", "4538b7111b121100763d91eebc0713f1");
    userGr.query();

    while (userGr.next()) {
        userGr.setValue('active', false); 
        userGr.update();
    }
}

 

Please mark my answer helpful and correct.

Regards,

Amit

Harish KM
Kilo Patron
Kilo Patron

Hi @yoli1 try below script

var userGr = new GlideAggregate("sys_user");
userGr.addEncodedQuery("active=true^u_country=4538b7111b121100763d91eebc0713f1");
userGr.addAggregate('COUNT');
userGr.query();

while(userGr.next()){

if (userGr.getAggregate('COUNT') > 500)

{

break;

 } else

{

 userGr.active = false;

userGr.update();

}

}

Regards
Harish