- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2019 07:50 AM
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.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2019 07:56 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2019 07:56 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2019 07:56 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2019 08:07 AM
Thank you so much! This worked exactly as planned!