Advanced reference qualifier to hide a value of a reference variable based on user's department

xhensilahad
Tera Expert

Hi everyone,

I have a requirement to hide a value of a reference field, if the users department starts with BT.

I have written the Script Include below:

 

Script Include:

var getUserDetails = Class.create();
getUserDetails.prototype = {
    initialize: function() {},
    // Method to get department filter based on user ID
    getFilter: function(userID) {
        var userGR = new GlideRecord('sys_user');
        if (userGR.get(userID)) {
            var department = userGR.department.name; 
            if (department && department.substring(0, 2) !== 'BT') {
                return 'active=true^name!=IT Infrastuktur';
            }
        }
    }
};
 
 
 
How do i call this Script include in my Advanced Reference qualifier script? Thank you
3 ACCEPTED SOLUTIONS

Simon Christens
Kilo Sage

try:

 

 

javascript: new getUserDetails().getFilter(gs.getUserID())

 

 

View solution in original post

Siddhesh2
Giga Guru

Hello @xhensilahad ,

 

You should uncheck the client callable option and use below code to call script include.

Siddhesh2_0-1732276505499.png

 

 

 

View solution in original post

I wouldn't worry too much about other system logs - you can drive yourself mad reading through all of that and it's likely unrelated.  You're getting the correct substring, so I guess we need to take this one step further to confirm what is being returned to the qualifier. And be sure to test both cases - so you should see this department name with this test case

var getUserDetails = Class.create();
getUserDetails.prototype = {
    initialize: function() {},
    getFilter: function(userID) {
		var userGR = new GlideRecord('sys_user');
        if (userGR.get(userID)) {
            var department = userGR.department.name.toString();
		if (department && department.substring(0, 2) === 'BT') {
                gs.addInfoMessage('SI inside if');
                return 'active=true';
            } else {
                gs.addInfoMessage('SI else');
                return 'active=true^name!=IT Infrastuktur';
            }
        }

    }
};

Then, if a non-BT... department user logs correctly in the else block, but still sees the department, hard-code the qualifier

active=true^name!=IT Infrastuktur

to see if this is correct.  You can manually filter a list view by this criteria to view the correct records, then right-click to copy the query and paste that into the qualifier/script.

View solution in original post

13 REPLIES 13

I have updated the *Script Include*

You can add gs.addInfoMessage() in your script include so that you can see that it gets called.

Then you can make some messages in your script to see where it goes wrong.

When you then click on the search icon on the field then you should see those messages

Your Reference qualifier should look like this:

BradBowman_0-1732271725041.png

 

Hi Brad, thank you for your response. 

Can you please review my Script Include as I can still see the variable in the Portal:

 

Script Include: (Client callable has been checked)

var getUserDetails = Class.create();
getUserDetails.prototype = {
    initialize: function() {},
    getFilter: function(userID) {
        var userGR = new GlideRecord('sys_user');
        gs.info(userGR);
        if (userGR.get(userID)) {
            var department = userGR.department.name;
            if (department && department.substring(0, 2) === 'BT') {
                return 'active=true';
            } else {
                return 'active=true^name!=IT Infrastuktur';
            }
        }

    }
};
 
 
Thank you

In this case Client callable is not required, but it doesn't hurt anything to have it checked.  Checking this box after a script exists does not add the extended object to the script, but even if it did it would still work with a qualifier.  You can temporarily add some lines like this to confirm the script is running, the value passed in from the qualifier, and the substring, then you should see where it is going wrong:

var getUserDetails = Class.create();
getUserDetails.prototype = {
    initialize: function() {},
    getFilter: function(userID) {
		gs.addInfoMessage('SI running ' + userID);
        var userGR = new GlideRecord('sys_user');
        if (userGR.get(userID)) {
            var department = userGR.department.name.toString();
			gs.addInfoMessage('SI dept ' + department);
			gs.addInfoMessage('si dept substring ' + department.substring(0, 2));
            if (department && department.substring(0, 2) === 'BT') {
                return 'active=true';
            } else {
                return 'active=true^name!=IT Infrastuktur';
            }
        }

    }
};