Need to send notification to requestor manager's manager if request manager is inactive

suuriya
Tera Contributor

Hi,

 

I have a requirement, created a scheduled jobs to trigger an event and that's sends a notification to the manger it is working fine but now i need to add a logic that the notification need to send to requestor's manager if requestor manager is active if not then notification need to be send to requestor manager's manager.

If Requestor's manager is active send to requestor manager (working)

If Requestor manager is inactive then send to requestor' manager's manager.

 

how we can achieve this

This is my script:

var asset_status;
var asset_date,cdate,dateDiff;
cdate = new GlideDate();
var ci = new GlideRecord('sc_req_item');
ci.addQuery('cat_item','47fef0cf1077a480594b72415810721e');
//ci.addQuery('sys_id','114385c8931c0690c8a5ba5d6cba104c');
ci.addActiveQuery();
ci.query();
while(ci.next())
{
    //gs.log("IDM test" + ci.variables.common_vars_requested_for.active);
    if(ci.variables.common_vars_requested_for.active !=true)
    {
    if(ci.variables.common_vars_requested_for.u_employee_type == 'Employee' && ci.variables.common_vars_requested_for.location.u_is_international !=true)
    {
        //gs.log("IDM test" + ci.variables.common_vars_requested_for.active);
    if(ci.variables.asset_status_change_date != '')
    {
        //gs.log("IDM inside if");
    asset_date = ci.variables.asset_status_change_date;
    var asset = new GlideRecord('alm_hardware');
var encodedQuery = 'model.u_sub_type!=VPC^assigned_to.nameSTARTSWITH' + ci.variables.common_vars_requested_for.name;
asset.addEncodedQuery(encodedQuery);
asset.query();
while (asset.next()) {
    if(asset.install_status =='106')
    asset_status = 'true';  
}
if(asset_status == 'true')
{
dateDiff = gs.dateDiff(asset_date,cdate);
if(dateDiff == '7')
gs.eventQueue("notify.manager.idm.second",ci,ci.variables.common_vars_requested_for.manager);
}
    }

    }
}

}
 
Thanks in Advance
4 REPLIES 4

Karunakaran
Mega Guru

Hi @suuriya ,

 

Steps to achieve this.

1. Create a function to check if the user is active or not. Let us say that the function name is "IsManagerActive(sysID)". In this method return true or false.

2.Create another function to get the Manager of Manager. Let us say the function name is "getManagersManager(sysID)". In this method return the Manager's Manager sysID.

3. You need to make a decision about the manager's active status inside the loop "if(dateDiff == '7')"

Example:

 

Replace the below 2 lines 

         if(dateDiff == '7')

          gs.eventQueue("notify.manager.idm.second",ci,ci.variables.common_vars_requested_for.manager);

 

with

if(dateDiff == '7')

{

          if(IsManagerActive(ci.variables.common_vars_requested_for.manager))

          {

          gs.eventQueue("notify.manager.idm.second",ci,ci.variables.common_vars_requested_for.manager);

          }

          else

          {

          var managersManager = getManagersManager(ci.variables.common_vars_requested_for.manager);

          gs.eventQueue("notify.manager.idm.second",ci, managersManager );

          }

}

 

 

 

Hope this helps. Please let me know, if you need any support for creating the functions.

Simply providing the script for everything will not teach any good lesson. that is why.

 

Please note that here we are not checking active status for the Manager's manager. If you want to check that, you need to create one method which works for all and loop it until you get an active record. Please let me know.

 

 

Regards,

Karunakaran.

HI @Karunakaran ,

 

Thanks for the reply

But i have tried like this 

if(dateDiff == '7')
{
    if(ci.variables.var_manager_name.active == true){
gs.eventQueue("notify.manager.idm.second",ci,ci.variables.common_vars_requested_for.manager);
    }
    else
    gs.eventQueue("notify.manager.idm.second",ci,ci.variables.common_vars_requested_for.manager.manager);
}
}
 
I believe it will also work right

 

Hi @suuriya 

 

Normally in server side this dot walking will work.

But, it also depends on the variable type. If it is a table object, it will work. please give a try.

 

But, it is not a generic solution in some cases where 1st two level of manager's status is inactive.

Please let me know, if you want to design this solution like that.

 

Otherwise you are good to proceed. Thank you.

 

Regards,

Karunakaran.

The best practice is using script include

 

var UserManager = Class.create();
UserManager.prototype = {
    initialize: function() {},

    getActiveManager: function(userId) {
        var user = new GlideRecord('sys_user');
        if (user.get(userId)) {
            // Get the user's manager
            var managerId = user.getValue('manager');
            if (managerId) {
                var manager = new GlideRecord('sys_user');
                if (manager.get(managerId)) {
                    // Check if the manager is active
                    if (manager.active) {
                        return manager;
                    } else {
                        // If the manager is inactive, recursively find the next active manager
                        return this.getActiveManager(manager.getUniqueValue());
                    }
                }
            }
        }
        // No active manager found
        return null;
    },

    type: 'UserManager'
};

 

Calling the script include from your script:

 

// Create an instance of the UserManager class
var userManager = new UserManager();

// Call the getActiveManager method to retrieve the active manager of a user
var userId = 'user_sys_id'; // Replace 'user_sys_id' with the sys_id of the user
var activeManager = userManager.getActiveManager(userId);

if (activeManager) {
    gs.info("Active Manager: " + activeManager.getValue('name') + ' | ' + activeManager);
gs.eventQueue("notify.manager.idm.second",ci,activeManager);
} else {
    gs.info("No active manager found for the user.");
}