Hide a reference variable option based on Users department
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2024 06:46 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2024 01:34 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2024 09:13 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2024 03:36 PM
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 ...