Deactivate users if not logged for 90 days and check if the users has any opened tickets

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi All,
I need assistance in scheduled job script to inactivate users who didn't log into system for 90 days and make them inactive. Also, check if the users inactivated has any opened tickets(Task Table), if yes create an incident to a dedicated group.
Can someone assist with the script please?
Thanks
Shan
- Labels:
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Steps to implement via Flow Designer:
-
Check last login:
-
Retrieve users from the User table.
-
Filter users whose last login is more than 90 days ago.
-
-
Check incident involvement:
-
Look up the Incident table.
-
Check if the user(s) is a caller of any incident where state = Active.
-
-
Perform action:
-
If the user is a caller on active incidents, update the incident as per the requirement.
-
Implementation tip:
-
Use the “Look Up Records” action in Flow Designer for checking conditions and fetching relevant records.
-
Execute steps sequentially to ensure proper logic flow.
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
3 weeks ago
You can use daily scheduled job with script like this, but please enhance
deactivateUsers();
function deactivateUsers() {
var dedicatedGroupSysId = 'group sysId';
var userRec = new GlideRecord("sys_user");
userRec.addActiveQuery();
userRec.addQuery("last_login", '<=', gs.daysAgo(90));
userRec.query();
while (userRec.next()) {
userRec.active = false;
userRec.update();
// logic to check if any open task for this user
var taskGR = new GlideRecord('task');
taskGR.addQuery('active', true);
taskGR.addQuery('opened_by', userRec.getUniqueValue());
taskGR.query();
var hasOpenTasks = taskGR.hasNext();
if (hasOpenTasks) {
// Create incident for open tasks assigned to dedicated group
var incidentGR = new GlideRecord('incident');
incidentGR.initialize();
incidentGR.short_description = 'User ' + userRec.name + ' inactive with open tasks';
incidentGR.description = 'User ' + userRec.name + ' has been inactivated but has open tasks. Please review and reassign as necessary.';
incidentGR.assignment_group = dedicatedGroupSysId;
incidentGR.category = 'Inquiry / Help'; // Customize as needed
incidentGR.insert();
}
}
}
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hope you are doing good.
Did my reply answer your question?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
You can create a Flow or Schedule job to deactivate the user if they have not logged in for specified number of days. Follow this link to set it up,
After setting 'active'=false for the user records, you can do a for each loop and check if they have any active incidents assigned to them and if yes, you can create a new incident and assign it to a support group.
Thanks,
Bhuvan