Join us Dec. 16 for The Community Hour, our first global virtual User Group event!

Get user and change filter from catalog item

User687957
Mega Guru

Dear all,

I have a wierd use case that I need to solve.
I have a catalog Item where I have a field name v_requested_for which refer to sys_user table.

Then I have define a variable set withe the following entries :
- Operation ( choice type) Add and Remove (operation)
- Role Reference to role table (role)

What I need to do is the following :

1 - From the onChange event of the operation variable of the variable set, I need to retrive the selected User ID from the v_requested_for field which is in the parent form.

2- My role reference is set to advance reference qualifier  with default filter which shown all roles. I need that when the operation change to "Remove", filter the reference role only from roles belonging to selected users.

Could you help me on how to do this from a client catlog script ?

I am stuck for 2 days actually

Thanks for sharing

Regards



2 REPLIES 2

NagaChandaE
Mega Sage

Hi @User687957 ,

You can't call parent form values directly in an advanced reference qualifier. g_service_catalog.parent.getValue() is a client-side API that only works in catalog client scripts. Reference qualifiers run server-side, and inside an MRVS, current.variables only sees that row's own variables. v_requested_for isn't one of them.

The fix is to copy the parent value into a variable inside the MRVS when the row opens. The reference qualifier can then read it through current.variables.

1. Add a user reference variable to the MRVS

  • Type: Reference
  • Name: mrvs_user
  • Reference: sys_user
  • Check Hidden on the variable so it doesn't show on the MRVS form.

This only hides it on the add/edit row form. The variable will still appear as a column in the MRVS table on the catalog item.

2. onLoad catalog client script on the MRVS

Set "Applies to" to the variable set.

 
javascript
function onLoad() {
    // g_service_catalog.parent gives access to variables on the parent catalog item from inside the MRVS
    var requestedFor = g_service_catalog.parent.getValue('v_requested_for');
    g_form.setValue('mrvs_user', requestedFor);
}

4. Script Include

Name it RoleFilterUtil and leave Client callable unchecked.

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

    getRoleFilter: function(operation, userId) {
        // Add (or empty): show all roles
        if (operation != 'remove')
            return '';

        // Remove without a user: show nothing
        if (!userId)
            return 'sys_idISEMPTY';

        var roles = [];
        var gr = new GlideRecord('sys_user_has_role');
        gr.addQuery('user', userId);
        gr.query();
        while (gr.next()) {
            roles.push(gr.getValue('role'));
        }

        roles = new ArrayUtil().unique(roles);
        return roles.length ? 'sys_idIN' + roles.join(',') : 'sys_idISEMPTY';
    },

    type: 'RoleFilterUtil'
};

5. Settings on the role variable

  • Advanced reference qualifier:
 
javascript: new RoleFilterUtil().getRoleFilter(current.variables.operation, current.variables.mrvs_user);
  • Variable attributes: ref_qual_elements=operation. This re-evaluates the qualifier when operation changes in the native UI. Service Portal evaluates it on each lookup anyway.

Change 'remove' to match the actual value of your Remove choice. To show only directly assigned roles, add gr.addQuery('inherited', false); to the Script Include.

Hope this helps. Please mark it as helpful or the accepted solution if it resolves your issue.

Ankur Bawiskar
Tera Patron

@User687957 

you can use advance reference qualifier and access the drop down variable value and accordingly apply filter

you can use script include for this

var RoleRefQualUtil = Class.create();
RoleRefQualUtil.prototype = {
    initialize: function() {},
    getRoles: function(operation, userId) {
        if (operation != 'remove')
            return '';                       // Add: show all roles
        if (!userId)
            return 'sys_idISEMPTY';          // no user selected: show nothing
        var ids = [];
        var gr = new GlideRecord('sys_user_has_role');
        gr.addQuery('user', userId);
        gr.addQuery('state', 'active');
        gr.query();
        while (gr.next())
            ids.push(gr.getValue('role'));
        return ids.length ? 'sys_idIN' + ids.join(',') : 'sys_idISEMPTY';
    },
    type: 'RoleRefQualUtil'
};

Ref qualifier like this

javascript: new RoleRefQualUtil().getRoles(current.variables.operation, current.variables.v_requested_for);

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