Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Hide the Related List if the manager don't have users.

LaraReddy
Tera Guru

Hi All,
Can anyone please us on the below script:

We wrote one script include and client script to hide the Related list, If the user/manager don't have any direct reporters. But some reasons it's not wokring for us, can anyone please check and let us know where we went wrong.

Script include:

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

    getActiveUsers: function(managerSysID) {
     var usr = new GlideRecord('sys_user');
        usr.addQuery('sys_id', current.manager);
       usr.query();
var userCount = 0;
        while (usr.next()) {
           userCount++;
            }
return userCount++;
},
type: 'getManagerDirectReports'
});


Client script:

function onLoad() {
 var usrs = new GlideAjax('getManagerDirectReports');
 usrs.addParam('sysparm_name','getActiveUsers');
usrs.getXML(callbackfunction);

 function callbackfunction(response)
 {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    alert(answer);  // Example: If one user have two reporting users but in alert we're getting zero

    if(answer == 0)
    {
        g_form.hideRelatedList('direct_reports');
    }
    else{
        g_form.showRelatedList('direct_reports');

    }
 }
}


Advance thanks.


1 ACCEPTED SOLUTION

Sorry my bad,

Please update script include like this:

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

    getActiveUsers: function(managerSysID) {
var manId = this.getParameter('manager_id');
     var usr = new GlideRecord('sys_user');
        usr.addQuery('manager', manId);  // updated query
       usr.query();
return usr.getRowCount();
},
type: 'getManagerDirectReports'
});
Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

11 REPLIES 11

Anil Lande
Kilo Patron

Hi,

Update your script include part like below:

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

    getActiveUsers: function(managerSysID) {
var manId = this.getParameter('manager_id');
     var usr = new GlideRecord('sys_user');
        usr.addQuery('sys_id', manId);
       usr.query();
return usr.getRowCount();
},
type: 'getManagerDirectReports'
});

 

And update your onLoad client script like below:

function onLoad() {
 var usrs = new GlideAjax('getManagerDirectReports');
 usrs.addParam('sysparm_name','getActiveUsers');
usrs.addParam('manager_id',g_form.getUniqueValue());
usrs.getXML(callbackfunction);

 function callbackfunction(response)
 {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    alert(answer);  // Example: If one user have two reporting users but in alert we're getting zero

    if(answer == 0)
    {
        g_form.hideRelatedList('direct_reports');
    }
    else{
        g_form.showRelatedList('direct_reports');

    }
 }
}
Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

HI Anil,
Thanks for the response.

Tried your suggestions, In one scenario user have two reporting users but in alert we're getting only one.

Advance thanks. 

Check user status, another user must be deactivated (active=false).

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

HI Anil,
Thanks again.

We thought the same but the reporting users are active only and one more thing there is one user with no direct repoters but in alert we're getting one as well.

Advance thanks.