- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
Hi
I am trying to get the last logins of user into servicenow portal who have ITIL role
The last login field on user table i am not sure whether it gives for backend or portal or for both
can anyone help on this
I want to remove only those users from ITil ROLE who have not logined in backend
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
last login tracks login info and doesn't tell if user logged in backend or portal
you can run a fix script or background script for your requirement.
Note: run this in DEV and test first
var inactiveDays = 90; // Adjust as needed
var grUser = new GlideRecord('sys_user');
grUser.addActiveQuery();
grUser.addQuery('roles', 'CONTAINS', 'itil'); // Has ITIL role
grUser.addQuery('last_login_time', 'RELATIVELE@dayofweek@ago@' + inactiveDays);
grUser.addQuery('user_name', 'NOT LIKE', 'Mid.Server'); // Exclude service accounts
grUser.query();
var count = 0;
while (grUser.next()) {
var grRole = new GlideRecord('sys_user_has_role');
grRole.addQuery('user', grUser.sys_id.toString());
grRole.addQuery('role.name', 'itil');
grRole.query();
while (grRole.next()) {
grRole.deleteRecord();
count++;
}
gs.info('Removed ITIL role from inactive user: ' + grUser.user_name);
}
gs.info('Total roles removed: ' + count);
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
last login tracks login info and doesn't tell if user logged in backend or portal
you can run a fix script or background script for your requirement.
Note: run this in DEV and test first
var inactiveDays = 90; // Adjust as needed
var grUser = new GlideRecord('sys_user');
grUser.addActiveQuery();
grUser.addQuery('roles', 'CONTAINS', 'itil'); // Has ITIL role
grUser.addQuery('last_login_time', 'RELATIVELE@dayofweek@ago@' + inactiveDays);
grUser.addQuery('user_name', 'NOT LIKE', 'Mid.Server'); // Exclude service accounts
grUser.query();
var count = 0;
while (grUser.next()) {
var grRole = new GlideRecord('sys_user_has_role');
grRole.addQuery('user', grUser.sys_id.toString());
grRole.addQuery('role.name', 'itil');
grRole.query();
while (grRole.next()) {
grRole.deleteRecord();
count++;
}
gs.info('Removed ITIL role from inactive user: ' + grUser.user_name);
}
gs.info('Total roles removed: ' + count);
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
- Navigate to the Table: Go to sys_user.list in the application navigator.
- Apply Filters:
- Active is true
- Roles contains itil
- Configure Columns: Add the Last login or Last login time field to the list view to see the data. (sort it to latest updated)
Alternative (Detailed Log): Query the sys_user_login_history table for more granular, historical data.
