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

Ankur Bawiskar
Tera Patron
Tera Patron

@ramireddypa 

not a good idea to have managers as select box (drop down).

Since 1st variable is reference you can select only 1 User and 1 User will have only 1 Manager

Why not have 2nd variable as reference and make it readonly?

You can use auto populate feature to populate and no scripting required

Auto-populate a variable based on a reference type variable (Utah)  

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello @Ankur Bawiskar ,

 

Thank you for responding.

On the user table, if there are 3 different users with same name then I would need to populate 3 user managers on the Manager field due to that I created select box variable

@ramireddypa 

but reference variable can hold only 1 user, so you expect user to select only 1 user

That user's manager can be auto populated in the next manager reference variable

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

mahesh009
Tera Expert

Hi @ramireddypa  This can be achieved by making the "Manager" variable as look up select box type. No Scripting required.
Change the "Manger" type to look up select box and under "Auto Populate section" select the dependent question as "User" and dot walk to manger field. 
This can solve your issue.

Thanks, and Regards

Mahesh

Please mark this response as correct or helpful if it assisted you with your question.