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.

Background Script

kusumgautam
Tera Contributor

How to write background script that will give you list of user whose location is empty.

 

1 ACCEPTED SOLUTION

Aniket Chavan
Tera Sage
Tera Sage

Hello @kusumgautam 

Please find the below background script to get the list of users whose location is empty.

var userGr = new GlideRecord('sys_user');
userGr.addQuery('location', '');
userGr.query();
var totalCount = 0;
while (userGr.next()) {
    gs.print('User with empty location: ' + userGr.getValue('name'));
    totalCount ++;
}

    gs.print('Total Count of users with empty location: ' + totalCount);


Please mark my answer helpful and correct if I have answered your question.

Thanks & Regards,
Aniket.

View solution in original post

9 REPLIES 9

Harish Bainsla
Kilo Patron
Kilo Patron

Hi write this code

var userQuery = new GlideRecord('sys_user');
userQuery.addNullQuery('location');
userQuery.query();

while (userQuery.next()) {
    gs.print('User Name: ' + userQuery.getValue('name') + ', Sys ID: ' + userQuery.getValue('sys_id'));
}
HarishBainsla_0-1702018737341.png

 

Community Alums
Not applicable

Hi,

You can refer this below script 

var userGr = new GlideRecord('sys_user');
userGr.addEncodedQuery('locationISEMPTY');
userGr.query();
while(userGr.next()){
    gs.print('Name = ' + userGr.name);
}
 

SarthakKashya2_0-1702020077242.png

 

Thanks 

Sarthak

 

Aniket Chavan
Tera Sage
Tera Sage

Hello @kusumgautam 

Please find the below background script to get the list of users whose location is empty.

var userGr = new GlideRecord('sys_user');
userGr.addQuery('location', '');
userGr.query();
var totalCount = 0;
while (userGr.next()) {
    gs.print('User with empty location: ' + userGr.getValue('name'));
    totalCount ++;
}

    gs.print('Total Count of users with empty location: ' + totalCount);


Please mark my answer helpful and correct if I have answered your question.

Thanks & Regards,
Aniket.

Kavita_Bhojane
Tera Guru

@kusumgautam 

 

You can use below script:

 

var usr = new GlideRecord("sys_user");
usr.addEncodedQuery("active=true^locationISEMPTY");
usr.query();
while(usr.next()){
gs.info("Users with empty locations: " + usr.name);
}
 
Screenshot 2023-12-08 at 6.27.57 PM.png
 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

Thanks,
Kavita Bhojane

kusumgautam
Tera Contributor

I got The solution than you for the help.