auto populate multiple users in a field

si21
Tera Guru

Hi experts,

We have a requirement to show all the reports of a  manager in a question. Not a reference field. All the users must be visible on the field without even selecting(like list collector).

si21_0-1728405941759.png

 

 

Is it feasible?

TIA

 

7 REPLIES 7

Brad Bowman
Kilo Patron
Kilo Patron

A List field / List Collector variable is your only choice for selecting more than one value.  onLoad or onChange of the selected manager field/variable, you could have the list field/variable populate all of the user records with that manager via a client script that calls a GlideAjax.

si21
Tera Guru

Hi @Brad Bowman  ,

I have tried using script include and onchange client script following community articles but list collector is not getting autopoulated. Could you please help me with the code?

Sure.  Post your scripts using the insert code icon </> and I'll take a look.

 

Here is the code which I used, error is saying as 'There is a JavaScript error in your browser console'

-----script include----------
var DirectReporteesUtil = Class.create();
DirectReporteesUtil.prototype = {
    initialize: function() {},

    getDirectReportees: function(managerSysId) {
        var reportees = [];
        var gr = new GlideRecord('sys_user');
        gr.addQuery('manager', managerSysId);
        gr.query();
        while (gr.next()) {
            reportees.push({
                sys_id: gr.sys_id.toString(),
                name: gr.name.toString()
            });
        }
        return JSON.stringify(reportees); // Return as JSON string
    },

    type: 'DirectReporteesUtil'
};


----clientscript---

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) {
        return; // Do nothing if the form is loading or no manager is selected
    }

    var ga = new GlideAjax('DirectReporteesUtil');
    ga.addParam('sysparm_name', 'getDirectReportees');
    ga.addParam('managerSysId', newValue); // Use the new manager's sys_id
    ga.getXMLAnswer(function(response) {
        var reportees = JSON.parse(response);
        var listCollector = g_form.getControl('list_of_direct_reportees'); // Replace with your List Collector field name

        // Clear existing values
        listCollector.value = '';

        // Populate the List Collector
        for (var i = 0; i < reportees.length; i++) {
            var option = new Option(reportees[i].name, reportees[i].sys_id);
            listCollector.add(option);
        }
    });
}