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.

Reference qualifier for two security groups.

Shree Nag
Tera Expert

Hello,

Appreciate your help.
I have a requirement to select users in a certain group into Manager field, when a 'Company" is selected in catalog form.
I have a Ref qualifier in Manager field written. I need "Group A" to be chosen for any and all companies A, B, C and D.

 

But when certain company say " Company C" is selected, users from 2 groups( Group A and Group B) needs to be displayed in manager feild.


How to pull up two groups here based on company" Company C".

 

 

javascript: 'active=true^company=' + current.variables.company + '^sys_idIN' + getIDs("3c292940472cfd10f726ba67436d43b5");
function getIDs(grp) {
var m = GlideUserGroup.getMembers(grp);
var ids = [];
while (m.next()) {
ids.push(m.getValue('user'));
}
return ids.toString();
}

4 REPLIES 4

OlaN
Tera Sage
Tera Sage

Hi,

Create a script include with a function that evaluates the input of the variable, and returns an encoded query to be used by the ref qualifier.

Chaitanya ILCR
Mega Patron

Hi @Shree Nag ,

 

try this

replace company c sysid and group 2 sysid with actual values

javascript & colon;'active=true^company=' + current.variables.company + '^sys_idIN' + getIDs("3c292940472cfd10f726ba67436d43b5") + ',' + (current.variables.company == 'COMPANY C SYSID' ? getIDs('GROUP 2 sysid') : '');

function getIDs(grp) {
    var m = GlideUserGroup.getMembers(grp);
    var ids = [];
    while (m.next()) {
        ids.push(m.getValue('user'));
    }
    return ids.toString();
}

I agree with @OlaN  better wrap this in a script include if just for one company as you have stated you can try the code I have shared if you want to scale it up further better wrap this up into a script include

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Shree Nag 

try this in reference qualifier

If you want you can move this to script include function and call it from reference qualifier

javascript: 'active=true^company=' + current.variables.company + '^sys_idIN' + getIDs(current.variables.company);

function getIDs(company) {
    var ids = [];
    if (company == 'Company C SysId') {
        ids = ids.concat(getGroupMembers('Group A SysId'));
        ids = ids.concat(getGroupMembers('Group B SysId'));
    } else {
        ids = getGroupMembers('Group A SysId');
    }
    return ids.toString();
}

function getGroupMembers(group) {
    var m = GlideUserGroup.getMembers(group);
    var ids = [];
    while (m.next()) {
        ids.push(m.getValue('user'));
    }
    return ids;
}

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

sunil maddheshi
Tera Guru

@Shree Nag Please try with below updated code:

(function() {
    var company = current.variables.company;
    var userIds = [];

    // Group sys_ids
    var groupA = '3c292940472cfd10f726ba67436d43b5'; // Group A
    var groupB = 'replace with group B sysid'; // Group B sysid

    // For Companies A, B, C, D -> Default to Group A
    if (company == 'company_sys_id_A' || company == 'company_sys_id_B' || company == 'company_sys_id_C' || company == 'company_sys_id_D') {
        userIds = getMembers(groupA);

        // Special case for Company C
        if (company == 'company_sys_id_C') {
            userIds = userIds.concat(getMembers(groupB));
        }
    }

    return 'active=true^sys_idIN' + userIds.join(',');
    
    function getMembers(groupSysId) {
        var ids = [];
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('group', groupSysId);
        gr.query();
        while (gr.next()) {
            ids.push(gr.user.toString());
        }
        return ids;
    }
})();

Please mark correct/helpful if this helps you!