I need to Fetch caller/user's manager and manager's manager and so on till top level until the last manager whose manager should be empty

bhanu27
Tera Expert

Hi Leads, 

i had a requirement below, please help on this.

  • caller creates a incident.
  • Background process (a rule or client script) looks at caller's manager ànd  manager’s manager so on …  till top level until the last manager whose manager should be empty.
  • But using below script i can able to loop till third level, is any feature in snow restricting my function, or my script is wrong please help.

Client Script: 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

var callerID = g_form.getValue('caller_id');
var si =new GlideAjax('getCallerID');
si.addParam('sysparm_name','getUserInfo');
si.addParam('sysparm_caller_id',callerID);
//si.getXML(getManager);
si.getXML(getUserInfo);
//function getManager(response){
function getUserInfo(response){

var manager = response.responseXML.documentElement.getAttribute('answer');
alert("Manager Name: "+ manager);

}
}

Script Include : 

 

var getCallerID = Class.create();
getCallerID.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getUserInfo: function() {
var userID = this.getParameter('sysparm_caller_id');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id',userID);
gr.query();
gr.next();
if(gr.manager!=''){
//while(gr.next()!=''){
for (i = 0; i < gr.next(); i++)
{
manager = gr.manager.getRefRecord().getDisplayValue('manager');
}
// var i= 0;
// do{
// //if(i<1){
// if(gr.manager()!=''){
// manager = gr.manager.getRefRecord().getDisplayValue('manager');
// return manager;
// }
// i++;
// }while(i<5);

return manager;
}
},
type: 'getCallerID'
});

12 REPLIES 12

johnfeist
Mega Sage
Mega Sage

Hi Bhanu,

If you need to go up the management chain until you find a person with no manager, the simplest thing would be to run your loop based on manager != ''. 

var userId = this.getParameter("sysparm_caller_id");
var gr = new GlideRecord("sys_user);
gr.get(userID);  // positions you at the record
var manager = gr.manager.getDisplayValue();
while (gr.manager != '') {
   gr.get("sys_id", gr.manager);
   if (gr.manager != '') {
      manager = gr.manager.getDisplayValue();
   }
}
return manager;

I haven't tested this, so I've probably left some fat fingers in it.  This should get yo on the right track.  You might want to add a first check for whoever has no manager so that you can return a value to indicate that.

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Hi @johnfeist

I have implemented your script in my PDI, it is giving null value.

 

Regards

Bhanu.

 

Willem
Giga Sage
Giga Sage

Can you try this:

- Create a function that will try and get the manager of the manager.

- If it does not have a manager, return empty

- In your original function check while it still finds a manager

- Each time it finds a manager, update the top manager

- When it does not find a manager for that manager, we have the top manager and can return that.

 

[update]: Can you try this:

var getCallerID = Class.create();
getCallerID.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserInfo: function () {
        var userID = this.getParameter('sysparm_caller_id');
        var manager = '';
        var topManager = '';
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', userID);
        gr.query();
        while (gr.next()) {
            manager = this.checkManager(userID);
            while(manager != topManager){
                manager = this.checkManager(manager);
                if(manager){
                    topManager = manager;
                }
            }
            return topManager;
        }
    },
    checkManager: function (user) {
        var gr = new GlideRecord('sys_user');
        gr.get(user)
        if(gr.manager){
            return gr.manager.getDisplayValue();
        }
        else{
            return '';
        }
    },


    type: 'getCallerID'
});

Hi @Willem thanks for your reply.

I have used your script, but it is giving the manager value as null.

As the top manager didn't have any manager, so it should provide the top-1 manager details.

Thanks.