- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2023 06:53 AM
How to write background script that will give you list of user whose location is empty.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 11:28 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2023 09:51 AM
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
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]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 09:47 PM
var gr = new GlideRecord('sys_user');
gr.addEncodedQuery('locationISEMPTY()');
gr.query();
while (gr.next())
{
gs.print(gr.user_name);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 10:01 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 10:55 PM
you can easily do this by applying filter condition and export it as excel and you need not use any script
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader