We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

ServiceNow Incident Auto-Reassignment to Active Manager Hierarchy

NegiSNOW
Tera Contributor

How can I write a ServiceNow script to check whether incidents are assigned to active users, and if the assigned user is inactive, automatically reassign the incident to that user’s manager? If the manager is also inactive, how can the script continue moving up the management hierarchy (up to four levels) until it finds an active manager to assign the incident to?

2 ACCEPTED SOLUTIONS

BharatC
Tera Contributor

Hello @NegiSNOW 

 

You can create scheduled job script and apply below script

 

(function() {

    // Query incidents assigned to inactive users
    var inc = new GlideRecord('incident');
    inc.addActiveQuery();
    inc.addNotNullQuery('assigned_to');
    inc.query();

    while (inc.next()) {

        var user = inc.assigned_to.getRefRecord();

        // If assigned user is inactive
        if (!user.active) {

            var manager = user.manager;
            var level = 0;
            var maxLevels = 4;
            var foundActiveManager = false;

            while (manager && level < maxLevels) {

                var mgr = manager.getRefRecord();

                if (mgr.active) {
                    // Reassign incident
                    inc.assigned_to = mgr.sys_id;
                    inc.work_notes = "Auto-reassigned because original assignee was inactive.";
                    inc.update();
                    foundActiveManager = true;
                    break;
                }

                // Move up hierarchy
                manager = mgr.manager;
                level++;
            }

            if (!foundActiveManager) {
                gs.log("No active manager found within 4 levels for incident " + inc.number);
            }
        }
    }

})();
 
 
Please mark this response as Helpful & accept it as solution if it assisted you with your question.
Regards

View solution in original post

VaishnaviK3009
Tera Guru

Hi @NegiSNOW !!

 

You can achieve this using a Before Business Rule on the Incident table.

 

  • Table: Incident

  • When: Before

  • Insert:

  • Update:

  • (Optional condition: Assigned To changes)

(function executeRule(current, previous) {

    if (!current.assigned_to) {
        return;
    }

    var userGR = new GlideRecord('sys_user');
    if (!userGR.get(current.assigned_to)) {
        return;
    }

    // If assigned user is active, no action needed
    if (userGR.active) {
        return;
    }

    var manager = userGR.manager;
    var level = 0;
    var maxLevels = 4;

    while (manager && level < maxLevels) {

        var managerGR = new GlideRecord('sys_user');
        if (!managerGR.get(manager)) {
            break;
        }

        if (managerGR.active) {
            current.assigned_to = managerGR.sys_id;
            break;
        }

        manager = managerGR.manager;
        level++;
    }

})(current, previous);

 

Mark this as Helpful if it clarifies the issue.
Accept the solution if this answers your question.

Regards,
Vaishnavi
Associate Technical Consultant

 

View solution in original post

3 REPLIES 3

Tanushree Maiti
Giga Sage

Hi NegiSNOW,

You can write an after/update business rule on user table . Do recursive call  to continue moving up management hierarchy.  This is just a sample code. (not tested)

condition: active changes to false

Script: 

function activeAssignedTo(current.sys_id);

var count =0;

function activeAssignedTo (UserID)

{

var gr = new GlideRecord('incident');

gr.addQuery('active', true);

gr.addQuery('assigned_to.sys_id', ''+UserID);

gr.setWorkflow(false);//use it if you do not need any other BRs to trigger

gr.query();

while(gr.next())

{

if (current.manager.active && count <4){

gr.assigned_to=current.manager;

gr.update();}

else

{

activeAssignedTo(current.manager);

count= count+1;

}

}

 

}

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

BharatC
Tera Contributor

Hello @NegiSNOW 

 

You can create scheduled job script and apply below script

 

(function() {

    // Query incidents assigned to inactive users
    var inc = new GlideRecord('incident');
    inc.addActiveQuery();
    inc.addNotNullQuery('assigned_to');
    inc.query();

    while (inc.next()) {

        var user = inc.assigned_to.getRefRecord();

        // If assigned user is inactive
        if (!user.active) {

            var manager = user.manager;
            var level = 0;
            var maxLevels = 4;
            var foundActiveManager = false;

            while (manager && level < maxLevels) {

                var mgr = manager.getRefRecord();

                if (mgr.active) {
                    // Reassign incident
                    inc.assigned_to = mgr.sys_id;
                    inc.work_notes = "Auto-reassigned because original assignee was inactive.";
                    inc.update();
                    foundActiveManager = true;
                    break;
                }

                // Move up hierarchy
                manager = mgr.manager;
                level++;
            }

            if (!foundActiveManager) {
                gs.log("No active manager found within 4 levels for incident " + inc.number);
            }
        }
    }

})();
 
 
Please mark this response as Helpful & accept it as solution if it assisted you with your question.
Regards

VaishnaviK3009
Tera Guru

Hi @NegiSNOW !!

 

You can achieve this using a Before Business Rule on the Incident table.

 

  • Table: Incident

  • When: Before

  • Insert:

  • Update:

  • (Optional condition: Assigned To changes)

(function executeRule(current, previous) {

    if (!current.assigned_to) {
        return;
    }

    var userGR = new GlideRecord('sys_user');
    if (!userGR.get(current.assigned_to)) {
        return;
    }

    // If assigned user is active, no action needed
    if (userGR.active) {
        return;
    }

    var manager = userGR.manager;
    var level = 0;
    var maxLevels = 4;

    while (manager && level < maxLevels) {

        var managerGR = new GlideRecord('sys_user');
        if (!managerGR.get(manager)) {
            break;
        }

        if (managerGR.active) {
            current.assigned_to = managerGR.sys_id;
            break;
        }

        manager = managerGR.manager;
        level++;
    }

})(current, previous);

 

Mark this as Helpful if it clarifies the issue.
Accept the solution if this answers your question.

Regards,
Vaishnavi
Associate Technical Consultant