Hide a reference variable option based on Users department

xhensilahad
Tera Expert

Good afternoon,

I have a requirement to hide an option (in a reference variable), if the users department starts with BT.

I have created this script Include and On Load script, but I am not sure how to progress with hiding the value now.

The variable's name is 'u_application', it is referencing to 'u_emergency_incident' table, and the option I want to hide is named  'IT'.

 

Script Include

var getUserDetails = Class.create();
getUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getDetails: function() {
        var user_sysid = this.getParameter('sysparm_caller');
        var user = new GlideRecord('sys_user');
        user.addQuery('sys_id', user_sysid);
        user.query();
        if (user.next()) {
            var userDepartment = user.department.getDisplayValue();
            return userDepartment;
        }
    },
    type: 'getUserDetails'
});
 
On Load Client Script:
function onLoad() {
    //Type appropriate comment here, and begin script below
    var userID = g_user.userID;
    var user = new GlideAjax("getUserDetails");
    user.addParam('sysparm_name', 'getDetails');
    user.addParam('sysparm_caller', userID);
    user.getXML(getResources);

    function getResources(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer.substring(0, 2) === "BT") {
         alert("The string starts with 'BT'");
}      
    }
}
 
 
 
Thank you
7 REPLIES 7

Add the following to the reference field you're looking to filter, within the Reference qual field - 

javascript: new global.getUserDetailz().getDetails();



Script Include - 

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

	getDetails: function() {
		var userList = [];

        var userGR = new GlideRecord('sys_user');
        userGR.get(gs.getUserID());
        userGR.query();

		
        if (userGR.next()) {
			
            var userDepartment = userGR.getDisplayValue('department');
        }
		if(userDepartment.startsWith("BT")){
			var servicesGR = new GlideRecord("table you're looking to filter");
			servicesGR.addEncodedQuery(Encoded query on how you want to filter);
			servicesGR.query();

			while(servicesGR.next()){
				userList.push(servicesGR.getUniqueValue());
			}
			return 'sys_idIN' + userList.join(",");
		} else {
			var servicesGR2 = new GlideRecord("table you're looking to filter");
			servicesGR2.query();

			while(servicesGR2.next()){
				userList.push(servicesGR2.getUniqueValue());
		}
			return 'sys_idIN'+ userList.join(",");
		}
    },

    type: 'getUserDetailz'
};

 

Alternatively, you could write out the specific filter condition on the reference qualifier but either works. 

Aniket Chavan
Tera Sage
Tera Sage

Hello @xhensilahad ,

 

To hide the option 'IT' from the reference variable 'u_application' based on the user's department starting with "BT," you can modify the On Load Client Script as follows:

 

Updated Script Include:

var getUserDetails = Class.create();
getUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getDetails: function() {
        var user_sysid = this.getParameter('sysparm_caller');
        var user = new GlideRecord('sys_user');
        user.addQuery('sys_id', user_sysid);
        user.query();
        if (user.next()) {
            return user.department.getDisplayValue();
        }
    },
    type: 'getUserDetails'
});

 

Updated On Load Client Script:

function onLoad() {
    var userID = g_user.userID;
    var ga = new GlideAjax("getUserDetails");
    ga.addParam('sysparm_name', 'getDetails');
    ga.addParam('sysparm_caller', userID);
    ga.getXML(handleResponse);

    function handleResponse(response) {
        var department = response.responseXML.documentElement.getAttribute("answer");

        // Check if the department starts with 'BT'
        if (department && department.startsWith("BT")) {
            // Remove the 'IT' option from the reference variable
            g_form.removeOption('u_application', 'IT');
        }
    }
}

 

Please give a try with the above updated code and let me know how it works.

 


Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.


Regards,
Aniket

Hi Aniket, thank you for your response unfortunately the remove Option is no longer supported as per this KB: removeOption does not worth through client script access to variable fields on task records - Known ...