How to Display users who report to a specific manager by selecting the manager's name?

Ramanjaneyuv
Tera Contributor

How to Display users who report to a specific manager by selecting the manager's name?

Scenario: 

  1. Create a Variable (manager ) Reference field and one more variable multiple line text field  
  2. When user enters manager It should fetch all the users reporting to the selected manager in multiline text field.

Please help me on this . 

Thanks.

2 REPLIES 2

Ehab Pilloor
Mega Sage

Hi @Ramanjaneyuv,

 

You can use List Collector variable as a better alternative to multi line text. You need to add a Client Script and use Glide Record while querying for current user in manager field. Use while loop to add all the names of the record whose manager is current user. 

 

If you found this reply useful, please mark it as solution and helpful.

Feel free to reach out for more.

 

Thanks and Regards,

Ehab

Dnyaneshwaree
Mega Sage

Hello @Ramanjaneyuv ,

Please refer below code for a example and update it as per your req:
Client script:

(function() {
  // Ensure the script runs when the 'manager' field changes
  function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
      return;
    }

    // Perform a GlideAjax call to fetch users reporting to the selected manager
    var ga = new GlideAjax('FetchUsersReportingToManager');
    ga.addParam('sys_id', newValue);
    ga.getXMLAnswer(function(response) {
      var answer = response.responseXML.documentElement.getAttribute('answer');
      g_form.setValue('users', answer);
    });
  }
})();

Script include:

var FetchUsersReportingToManager = Class.create();
FetchUsersReportingToManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  getUsers: function() {
    var managerId = this.getParameter('sys_id');
    var users = [];

    var gr = new GlideRecord('sys_user');
    gr.addQuery('manager', managerId);
    gr.query();

    while (gr.next()) {
      users.push(gr.name.toString());
    }

    return users.join('\n');
  }
});

 

Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru