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.

Need help with population of roles and groups in catalog item

shaik riyaz1
Tera Contributor

Hi,

 

I’m working on a catalog item where I need to configure two fields with the below behavior:

 

Field Name: Roles assigned to the user

 

Requirement: It should show the list of all roles assigned to the logged-in user(Requested for) field, basically fetched from the sys_user_has_role table.

 

Field Name: Groups associated with the selected role

 

Type: Multi-select dropdown

 

Requirement:

 

When a role is selected in the “Roles assigned to user” field,

the system should display all groups associated with that role (in which the user is a member).

 

All those groups should be auto-selected by default, but the user should be able to deselect if needed.

 

I’m trying to implement this logic using a catalog client script and script include, but I’m not sure about the best approach to dynamically populate the multi-select dropdown based on the selected role.

 

Has anyone implemented something similar or can guide on the right way to achieve this (preferably without using gel or DOM methods)?

 

Thanks

 

1 ACCEPTED SOLUTION

@shaik riyaz1 

Ensure the default value for requested_for has gs.getUserID() so that logic works when form loads for logged in user and then it will work based on user change

if you want based on requested for variable and user will select that then do this

javascript: var query;
var cu = gs.getUser().getUserByID(current.variables.requested_for);
query = 'nameIN' + new global.ArrayUtil().convertArray(cu.getRoles());
query;

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

7 REPLIES 7

@shaik riyaz1 

Ensure the default value for requested_for has gs.getUserID() so that logic works when form loads for logged in user and then it will work based on user change

if you want based on requested for variable and user will select that then do this

javascript: var query;
var cu = gs.getUser().getUserByID(current.variables.requested_for);
query = 'nameIN' + new global.ArrayUtil().convertArray(cu.getRoles());
query;

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Hi @Ankur Bawiskar ,

The code is working fine for the 'roles assigned to the user' , Thank you

 

Regarding #2 i have gone through the blog and my requirement is 'For the selected role in the 'Roles assigned to user' field, the system will display all groups associated with that role in which the user is a member.
All the groups related to that role will be auto-selected, which can be deselected by User if needed' 

I have tried with below code but its not working. It always shows all groups. Also, I think I am unable to clear all options , tried clearValue(), clearOptions().

Script include:

getGroupsForRole: function() {
        var roleSysId = this.getParameter('sysparm_role');
        gs.info('role is ' + roleSysId);
        var result = [];
 
        if (!roleSysId)
            return JSON.stringify(result);
 
        var gr = new GlideRecord('sys_group_has_role');
        gr.addQuery('role', roleSysId);
        gr.query();
        if(gr.next()){
            var group = gr.group.getRefRecord();
            var groupName = group.name + '';
            gs.info('group name is ' + groupName);
            if (groupName) {
                result.push({
                    sys_id: gr.group.toString(),
                    name: groupName
                });
            }
       
        }

 
        return JSON.stringify(result);
    },

    type: 'UserRoleFetcher'
 

});

Catalog client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
 
    // Clear existing options
    g_form.clearValue('groups_associated_with_the_selected_role');
 
    // Call GlideAjax to get groups linked to selected role
    var ga = new GlideAjax('UserRoleFetcher');
    ga.addParam('sysparm_name', 'getGroupsForRole');
    ga.addParam('sysparm_role', newValue);
 
    ga.getXMLAnswer(function(response) {
        alert('response from script is ' + Object.keys(response).length);
        if (Object.keys(response).length >2) {
            var groups = JSON.parse(response);
            groups.forEach(function(group) {
                g_form.addOption('groups_associated_with_the_selected_role', group.sys_id, group.name);
            });
        }
        else{
            alert('inside else block');
            g_form.clearValue('groups_associated_with_the_selected_role');

        }
    });
}
 

@shaik riyaz1 

2nd variable I recommended to have list collector type referring to sys_user_group

Only list collector allows you multi-selection

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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