- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2025 11:16 AM
I have implemented a scheduled job in ServiceNow to track and manage inactive users based on their last login time. This ensures that inactive users are properly monitored and notified before deactivation. However, the final notification is not being received by users when using the following query:
Here, date60 is a GlideDate variable that retrieves all users who have not logged in for more than 60 days, and grInactive is a GlideRecord variable.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2025 09:26 PM - edited 03-07-2025 09:27 PM
Here's my approach and it's working as expected.
1. Event registry:
2. Scheduled job:
var user = new GlideRecord('sys_user');
//Active users with last login time is not empty and last login time is before 60 days.
user.addEncodedQuery("active=true^last_login_timeISNOTEMPTY^last_login_timeRELATIVELT@dayofweek@ago@60");
user.query();
while (user.next()) {
//Triggering the event by passing the user record and user sys_id
gs.eventQueue('dormancy.policy.trigger', user, user.sys_id);
// Calling the function to remove user from the groups
this.removeUsersFromGroup(user);
}
// Function to remove users from the group
function removeUsersFromGroup(user_record) {
var group_membership = new GlideRecord('sys_user_grmember');
group_membership.addQuery("user", user_record.sys_id);
group_membership.deleteMultiple();
}
Notification:
Result:
I hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2025 04:34 PM - edited 03-06-2025 04:46 PM
1. Are you sure that users are active at that time of triggering this event?
2. Please validate your encoded query, because you've mentioned date60 is GlideDate variable but last_login_time is GlideDateTime variable. So your encoded filter condition would be something like below,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2025 10:50 AM
I have tried the this: last_login_timeRELATIVELT@dayofweek@ago@60, But my notification still not triggering and also the user is active, I am sending notification before taking the roles and permission however my notification is not getting even my event is triggering also roles and groups are taking properly,
Here is my Whole code,
I have test multiple scenario that's why code is not clean.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2025 09:26 PM - edited 03-07-2025 09:27 PM
Here's my approach and it's working as expected.
1. Event registry:
2. Scheduled job:
var user = new GlideRecord('sys_user');
//Active users with last login time is not empty and last login time is before 60 days.
user.addEncodedQuery("active=true^last_login_timeISNOTEMPTY^last_login_timeRELATIVELT@dayofweek@ago@60");
user.query();
while (user.next()) {
//Triggering the event by passing the user record and user sys_id
gs.eventQueue('dormancy.policy.trigger', user, user.sys_id);
// Calling the function to remove user from the groups
this.removeUsersFromGroup(user);
}
// Function to remove users from the group
function removeUsersFromGroup(user_record) {
var group_membership = new GlideRecord('sys_user_grmember');
group_membership.addQuery("user", user_record.sys_id);
group_membership.deleteMultiple();
}
Notification:
Result:
I hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2025 04:53 PM
Hi thanks for this let me try your code..