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

Shankar Manohar
Mega Guru

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

 

 

6 REPLIES 6

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Shankar Manohar 

Steps to implement via Flow Designer:

  1. Check last login:

    • Retrieve users from the User table.

    • Filter users whose last login is more than 90 days ago.

  2. Check incident involvement:

    • Look up the Incident table.

    • Check if the user(s) is a caller of any incident where state = Active.

  3. 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]

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Shankar Manohar 

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.

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

@Shankar Manohar 

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.

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

Bhuvan
Tera Sage

@Shankar Manohar 

 

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,

 

https://www.servicenow.com/community/itsm-forum/how-to-disable-a-user-if-the-last-login-time-is-grea...

 

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