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

Dr Atul G- LNG
Tera Patron
Tera Patron

HI @kusumgautam 

 

(function() {
// Check if the current user's location is empty
var userLocation = gs.getUser().getLocation();

if (!userLocation) {
gs.info('User location is empty');
// Perform actions here if the location is empty
} else {
gs.info('User location is available');
// Perform other actions if the location is available
}
})();

or

 

LearnNGrowAtul_0-1701885059101.png

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

cpawan
Tera Expert

var gr = new GlideRecord('sys_user');
gr.addEncodedQuery('locationISEMPTY()');

gr.query();
while (gr.next())

{
gs.print(gr.user_name);

}

Danish Bhairag2
Tera Sage
Tera Sage

Hi @kusumgautam ,

 

To create a background script in ServiceNow that retrieves a list of users with empty locations, you can use GlideRecord queries. Here's a simple example:

 

```javascript

// Create a new GlideRecord for the 'sys_user' table (User table)

var userGR = new GlideRecord('sys_user');

 

// Add a query to find users with empty 'location' field

userGR.addQuery('location', ''); // Assuming 'location' is the field you want to check

 

// Execute the query

userGR.query();

 

// Iterate through the results

while (userGR.next()) {

    // Output user details or perform any desired actions

    gs.info('User without location: ' + userGR.getValue('name'));

}

```

 

Make sure to replace `'location'` with the actual field name you want to check for emptiness. You can customize the output or actions inside the loop based on your specific requirements.

 

Remember to test your script in a safe environment before running it in production to ensure it behaves as expected and doesn't unintentionally modify records.

 

Thanks,

Danish

 

Ankur Bawiskar
Tera Patron
Tera Patron

@kusumgautam 

you can easily do this by applying filter condition and export it as excel and you need not use any script

AnkurBawiskar_0-1702018505554.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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