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 check if list type field contains all inactive users

shweta14
Tera Contributor

Hi Community,

 

I have requirement where i have to check if list type field contains all inactive users

please correct my below code
in this code i'm getting any one is inactive user but i want all inactive then show msg

var gr = new GlideRecord('sys_user');
        gr.addQuery("sys_id", "IN", listfieldValue);
        gr.addQuery('active', false);
        gr.query();
        if (gr.next()) {
            if (gr.active == false) {
              Fields = {
               gs.print("all users are inactive");
            };
            }
1 ACCEPTED SOLUTION

Onkar Pandav
Tera Guru

Try below code:

        var listField = current.list_field.toString();
	count = 0;
	for (i=0;i<=listField.lenght;i++) {
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id', listField[i]);
		gr.addQuery('active', true);
		gr.query();
		if (gr.next()) {
			count++;
		}
	}
	if (count == 0)
		gs.addErrorMessage("All users are inactive");

View solution in original post

4 REPLIES 4

James Chun
Kilo Patron

Hi @shweta14,

 

No need to iterate through the whole list, try something like below:

var gr = new GlideRecord('sys_user');
gr.addQuery("sys_id", "IN", listfieldValue);
gr.addQuery('active', true);
gr.query();
if(gr.getRowCount()== 0)
{
    gs.print("All users are inactive");
}

Manoj89
Giga Sage

Hi Shwetha,

 

Try this, since list collector return sysIds, I have used sysIds in the query part

 

 

var r = new GlideAggregate('sys_user');
r.addQuery('sys_id', 'IN', 'a8f98bb0eb32010045e1a5115206fe3a,71826bf03710200044e0bfc8bcbe5d3b,12826bf03710200044e0bfc8bcbe5db1');
r.addAggregate('COUNT', 'active');
r.query();
while(r.next()){
	gs.print(r.active + ' ' + r.getAggregate('COUNT', 'active'));
}

Harish KM
Kilo Patron
Kilo Patron

Hi @shweta14 to look multiple users you need to use while condition instead of if

if (gr.next()) { // replace to while

 

here is sample code

var listfieldValue = 'sys_idIN62826bf03710200044e0bfc8bcbe5df1,a8f98bb0eb32010045e1a5115206fe3a,0a826bf03710200044e0bfc8bcbe5d7a';
var gr = new GlideRecord('sys_user');
gr.addEncodedQuery("sys_idIN"+listfieldValue);
gr.query();
while(gr.next()) {
    if (gr.active == false) {
       
            gs.info("all users are inactive");
       
    }
    }
Regards
Harish

Onkar Pandav
Tera Guru

Try below code:

        var listField = current.list_field.toString();
	count = 0;
	for (i=0;i<=listField.lenght;i++) {
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id', listField[i]);
		gr.addQuery('active', true);
		gr.query();
		if (gr.next()) {
			count++;
		}
	}
	if (count == 0)
		gs.addErrorMessage("All users are inactive");