Query for active and multiple

htank66
Kilo Contributor

Hello all,

I need coding help.  How would I select only users that are active and confirm there is only one user with that name?

Below is what I have so far:

var gr = GlideRecord('sys_user');
gr.addQuery('name', fullName);
gr.query();
if(gr.next()) {
	current.submitter = gr.sys_id;
}
else {
	current.submitter = " ";
}

Basically I want to select a user with the name being "fullName", being active, and being the only user with that name.  And if all of these things aren't true to return nothing to the field.

Please help.

1 ACCEPTED SOLUTION

Abhishek Pidwa
Kilo Guru

Hello , 

 

Firstly if you are querying by name , that could yield multiple results . Like if the name is John Doe there might be many with that name. Anyways, if that is what you want , you can simply use getRowCount function. Also you can add activeQuery to filter out only active records:

var gr = GlideRecord('sys_user');

gr.addActiveQuery(); //filter to only select active queries

gr.addQuery('name', fullName);

gr.query();

if (gr.getRowCount() == 1){

gr.next();

current.submitter = gr.sys_id;

}

else{

current.submitter = " ";

}

 

Hope this helps. Try this out and let me know if you have any questions.

 

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update code as below:

var gr = GlideRecord('sys_user');

gr.addQuery('name', fullName);

gr.addQuery('active', true);

gr.query();

var count = gr.getRowCount();

if(count == 1){

// there is only 1 user with that full name which is active

}

else{

// there could be more than 2 users with that same full name and are active

}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Abhishek Pidwa
Kilo Guru

Hello , 

 

Firstly if you are querying by name , that could yield multiple results . Like if the name is John Doe there might be many with that name. Anyways, if that is what you want , you can simply use getRowCount function. Also you can add activeQuery to filter out only active records:

var gr = GlideRecord('sys_user');

gr.addActiveQuery(); //filter to only select active queries

gr.addQuery('name', fullName);

gr.query();

if (gr.getRowCount() == 1){

gr.next();

current.submitter = gr.sys_id;

}

else{

current.submitter = " ";

}

 

Hope this helps. Try this out and let me know if you have any questions.

 

Thank you so much!  This worked exactly as planned!