Assign All the tasks and types of tickets to active user from inactive user

Swathi Gade
Tera Contributor

We need to get the inactive users and reassigns all their associated Incidents, Problems, Tasks, and Change Tickets to active users within the same group. Could someone help me with this

2 ACCEPTED SOLUTIONS

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Swathi Gade 

You can create a report on the TASK table with this condition, which will return all records where the assignment is inactive (active = false). You can use these records as a basis and manually assign them to the same group.

 

You can add filter task type =  Change or Problem or Incident

 

AGLearnNGrow_0-1744724210101.png

 

 

*************************************************************************************************************
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]

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

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@Swathi Gade 

something like this in scheduled job or background script

But please test in DEV and then only migrate to next instance

(function() {
    // Query inactive users
    var inactiveUsers = new GlideRecord('sys_user');
    inactiveUsers.addQuery('active', false);
    inactiveUsers.query();

    while (inactiveUsers.next()) {
        var userId = inactiveUsers.sys_id;
        var userGroup = inactiveUsers.getValue('sys_user_group'); // Assuming the user has a group field

        // Query all active users in the same group
        var activeUsers = new GlideRecord('sys_user');
        activeUsers.addQuery('active', true);
        activeUsers.addQuery('sys_user_group', userGroup);
        activeUsers.query();

        if (activeUsers.next()) {
            var newAssignee = activeUsers.sys_id;

            // Reassign Incidents
            reassignRecords('incident', userId, newAssignee);

            // Reassign Problems
            reassignRecords('problem', userId, newAssignee);

            // Reassign Tasks
            reassignRecords('task', userId, newAssignee);

            // Reassign Change Tickets
            reassignRecords('change_request', userId, newAssignee);
        }
    }

    function reassignRecords(tableName, oldAssignee, newAssignee) {
        var gr = new GlideRecord(tableName);
        gr.addQuery('assigned_to', oldAssignee);
        gr.addQuery('active', true); // Only reassign active records
        gr.query();

        while (gr.next()) {
            gr.assigned_to = newAssignee;
            gr.update();
        }
    }
})();

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

View solution in original post

2 REPLIES 2

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Swathi Gade 

You can create a report on the TASK table with this condition, which will return all records where the assignment is inactive (active = false). You can use these records as a basis and manually assign them to the same group.

 

You can add filter task type =  Change or Problem or Incident

 

AGLearnNGrow_0-1744724210101.png

 

 

*************************************************************************************************************
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

@Swathi Gade 

something like this in scheduled job or background script

But please test in DEV and then only migrate to next instance

(function() {
    // Query inactive users
    var inactiveUsers = new GlideRecord('sys_user');
    inactiveUsers.addQuery('active', false);
    inactiveUsers.query();

    while (inactiveUsers.next()) {
        var userId = inactiveUsers.sys_id;
        var userGroup = inactiveUsers.getValue('sys_user_group'); // Assuming the user has a group field

        // Query all active users in the same group
        var activeUsers = new GlideRecord('sys_user');
        activeUsers.addQuery('active', true);
        activeUsers.addQuery('sys_user_group', userGroup);
        activeUsers.query();

        if (activeUsers.next()) {
            var newAssignee = activeUsers.sys_id;

            // Reassign Incidents
            reassignRecords('incident', userId, newAssignee);

            // Reassign Problems
            reassignRecords('problem', userId, newAssignee);

            // Reassign Tasks
            reassignRecords('task', userId, newAssignee);

            // Reassign Change Tickets
            reassignRecords('change_request', userId, newAssignee);
        }
    }

    function reassignRecords(tableName, oldAssignee, newAssignee) {
        var gr = new GlideRecord(tableName);
        gr.addQuery('assigned_to', oldAssignee);
        gr.addQuery('active', true); // Only reassign active records
        gr.query();

        while (gr.next()) {
            gr.assigned_to = newAssignee;
            gr.update();
        }
    }
})();

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