If group manager is Inactive, set group manager's manager as the manager of the group

skumar_snow
Kilo Explorer

Hi Community

I need some help with the Script, can you please help me with the requirement below

I want to write a scheduled job

If the Group manager is inactive, set the group manager's manager as the manager of the group. In case if the group manager's manager is also inactive it should set the group manager's managers manager. In case if the new manager is a CEO or CIO or a CTO, the script should set the manager of the group as "Change manager group" manager.

Please let me know if you have any question.

 

Thanks in Advance

4 REPLIES 4

Maik Skoddow
Tera Patron
Tera Patron

Hi @skumar.snow ,

to my mind your use case is worth to be implemented as a Flow. This gives you the possibility to visualize all cases and not only the ones you have described. Consider that each "if" also have an "else" you have to handle. And with a graphical flow you can discuss the business requirement much better with the requester as with source code.

Kind regards
Maik

If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

AnirudhKumar
Mega Sage
Mega Sage

Using a scheduled Job for this will be very heavy for the system.

The optimal way to do this is by a business rule on sys_user table triggered on update when of the 'Active' field changes to false

 

Use this script inside your Business Rule:

 

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

var changeManagerGroupManager = '3e82abf03710200044e0bfc8bcbe5d24';//Change Manager Grp's Manager

var managerFound = false;
var newManager = '';
var higherManager = 'manager';

while (managerFound == false) {
if (current.getElement(higherManager)) {

if (current.getElement(higherManager).active == true) {

if (current.getElement(higherManager).title == 'CEO' || current.getElement(higherManager).title == 'CIO' || current.getElement(higherManager).title == 'CTO')
newManager = changeManagerGroupManager;
else
newManager = current.getElement(higherManager);

managerFound = true; //Stops while loop
}
} else
break;//If there's no manager for the inactivated user, then nothing happens

higherManager = higherManager + '.manager';

}


//Updating the manager fields
if (newManager) {
var group = new GlideRecord("sys_user_group");
group.addQuery("manager", current.sys_id);
group.query();
while (group.next()) {
group.manager = newManager;
group.update();
}
}

 

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

 

Note

- I've hardcoded the change group manager grp Manager's sysid. Please use a system property or a gliderecord as a better practice

- This script is not just for 2 levels of manager checks. It goes on checking until an active manager (non CxO) is found.

- The Title field is used to check if the manager is any CxO

- If there's no manager for the inactivated group's manager, then nothing happens.

 

 

 

Long live ServiceNow! 🙂

 

Hi Anirudh

 

I really appreciate for helping me on this.

If you dont mind I have a small correction in the script

 

I see you are passing the sys_id of the change manager group manager directly. That is not how I was looking for. It should set the Manager of the "Change management group" as  the manager of the group (on which the manager is Inactive).

It should get the  Manager of the  "Change management group" and set as the Manager of those group or groups on which the CIO or CTO should not be set as a manager of the group.

 

I am so sorry for the inconvenience

Thanks in advance

Hi Anirudh

I already have the BR script mentioned below, I am little confused to add the logic, if the new manager is going to be a CIO or CTO, the script should set the manager as "Change Management group"  group manager as the manager of the group.

Our requirement is to not set a CIO or CTO as a manager of the group. The group manager of the "Change management group" will take care of those groups

 

var grp = new GlideRecord("sys_user_group");
grp.addQuery("manager", current.sys_id);
grp.query();
while (grp.next() ) {
grp.manager = current.manager;
grp.update();
}