The CreatorCon Call for Content is officially open! Get started here.

Populate Manager variable based on selection of User variable

ramireddypa
Mega Contributor

I'm working on a catalog item that has two variables

1. User -> Reference to sys_user table

2. Manager -> Select Box

 

The requirement is when a user is selected then Managers select Box should be populated with the Manager(s) of that user.

Some Users may have the same display name. example: three users named Abel, but they have different managers. Therefore, I want to populate all three users managers on the Manager Variable. I tried but null value is passing onto the client side however on server side I'm getting all the managers. Could anyone please guide me where I did wrong.

 

Server Side Script:

 

var fetchManager = Class.create();
fetchManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getName: function() {
        var users = this.getParameter('sysparm_user');
        var nam = '';
        var grName = new GlideRecord('sys_user');
        if (grName.get(users)) {
            nam = grName.getDisplayValue();
        }
        var result = [];
        var gr = new GlideRecord('sys_user');
        gr.addQuery('name', nam);
        gr.addNotNullQuery('manager');
        gr.query();
        while (gr.next()) {
            var managerGr = gr.manager.getRefRecord();
            if (managerGr) {
                result.push({
                label: managerGr.getDisplayValue().toString(),
                value: managerGr.getDisplayValue().toString()
                });
        }  
        }
   
    gs.info("List of Managers" + JSON.stringify(result));
         return JSON.stringify(result);
    },
    type: 'fetchManager'
});
 
 
Client Side Script:
 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    } //Type appropriate comment here, and begin script below
 
    var ga = new GlideAjax('fetchManager');
    ga.addParam('sysparm_name', 'getName');
    ga.addParam('sysparm_user', newValue);
    ga.getXMLAnswer(function(answer) {
        alert(answer);
        if (!answer) {
            g_form.clearOptions('managers');
            return;
        }
        var accounts = JSON.parse(answer);
        alert(accounts);
        g_form.clearOptions('managers');
        g_form.addOption('managers', '', 'select manager', '--Select Manager--');
        for (var i = 0; i < accounts.length; i++) {
            alert(accounts[i].label);
            g_form.addOption('managers', accounts[i].value, accounts[i].label);
        }
    });
}
 
 
6 REPLIES 6

prerna_sh
Giga Sage
Giga Sage

Hi @ramireddypa 
There is no need of script, you can directly use "Auto-populate", with the same field type.

To auto-populate a field in, follow these steps:

  1. Open the variable you want to auto-populate (e.g., Manager).

  2. In the Auto-populate field, select the dependent question (e.g., User), this is the variable whose value will be used.

  3. Use dot-walking to fetch the desired value. For example, if you want to display the user’s manager’s first name, dot walk to: "Manage> First Name", And that’s it, your field will auto-populate
    Please check attached screen shots.

Also, you can go through the following:

https://www.youtube.com/watch?v=8yg1Mtu3JTE
https://www.servicenow.com/community/itsm-forum/autopopulate-the-variables-in-the-catalog-item-based...

---------------------------------------------------------------------------------------------------------------------------------------------------

If my response solves your query, please marked helpful by selecting Accept as Solution and Helpful. Let me know if anything else is required.

Thanks,

Prerna

Brad Bowman
Kilo Patron
Kilo Patron

In your testing, what does your "List of Managers" log show?  How about the alert on answer? And the alert on account? If the first is correct but answer or account is null / incorrect, you can simplify the array you are returning, since the value and the label are the same in this case, then you would have an array instead of an array of objects:

while (gr.next()) {
    var managerGr = gr.manager.getRefRecord();
    if (managerGr) {
        result.push(managerGr.getDisplayValue().toString());
    }
}     
ga.getXMLAnswer(function(answer) {
    alert(answer);
    if (!answer) {
        g_form.clearOptions('managers');
        return;
    }
    var accounts = answer.split(',');
    alert(accounts);
    g_form.clearOptions('managers');
    g_form.addOption('managers', '', 'select manager', '--Select Manager--');
    for (var i = 0; i < accounts.length; i++) {
        alert(accounts[i]);
        g_form.addOption('managers', accounts[i], accounts[i]);
    }
});

If you want to be able to actually select multiple Manager values, this variable must be a List Collector type.

 

When selecting one Manager for a user with a display name that is not unique, it would be more straight-forward / best / recommended to change the Manager variable to a reference type with a qualifier that calls the Script Include, passing in the User selected.

BradBowman_0-1758218722751.png

provided 'user' is the correct variable name.  Then you don't need an OnChange Client Script, and your Script Include would look more like this:

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

    getName: function(user) {
        var nam = '';
        var grName = new GlideRecord('sys_user');
        if (grName.get(user)) {
            nam = grName.getDisplayValue();
        }
        var result = [];
        var gr = new GlideRecord('sys_user');
        gr.addQuery('name', nam);
        gr.addNotNullQuery('manager');
        gr.query();
        while (gr.next()) {
            result.push(gr.getValue('manager'));
        }  
     
    	gs.info("List of Managers" + result.join(','));
        return 'sys_idIN' + result.join(',');
    },

    type: 'fetchManager'
};

This approach also has the benefit of the Manager variable value = a sys_id as is expected for user values, so it is easier to work with in your flow and/or reporting.