
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2022 05:57 AM
Hey there SN Comm Team,
I am in need of some help to hide certain choices from a custom field when the logged in user does not belong to the Human Resources Department.
The field and choices are on the HRIT Operations table
Department field sits on the user table (sys_user)
I also need to hide these choices on the portal side. The variable I am using is a reference type. Using a reference qualifier to reference the choices so that I did not have to duplicate all the choices (there are 37).
I believe I have to use both Script Include with Ajax and a Client Script, but having difficulty scripting it.
Can someone lend me a hand and show me the way 🙂
Thank you in advance,
-Rob
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2022 07:46 AM
Hello Rob,
If your requirement is to use a drop down field then I would suggest not to use the reference type field. You can use the Select box type (choice type) field which is a best practice, because if you have a requirement to create UI policy on this field or Hide other fields based on this field value if future, then it will be difficult for you or team to implement the UI Policy because the reference qualifier will not be applicable on condition field in UI policy table. So you we end up having 100s of value for your field when creating UI Policy.
If you are using the Select box type field then you can use the below code:
function onLoad() {
var ga = new GlideAjax("DepartmentUtilsCustom");
ga.addParam('sysparm_name', 'isUserFromHrDepartment');
ga.getXMLAnswer(getResources);
function getResources(response) {
var answer = response;
alert(answer);
if (!answer || answer == "false") {
var removeOptions = ["value1", "value2", "value3"]; // you can add the drop-down choice field value that you want to remove
for (var optionValues in removeOptions) {
g_form.removeOption("<YOUR_FIELD_NAME>", removeOptions[optionValues]);
}
}
}
}
Create a System property:
Name: hr.department.sys_id
Value: <sys_id of human resource department in your instance>
Script include:
var DepartmentUtilsCustom = Class.create();
DepartmentUtilsCustom.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isUserFromHrDepartment: function () {
// As the department of logged in user will not change frequently we can get the Department ID of logged user from user's current session
return (gs.getUser().getDepartmentID() == gs.getProperty("hr.department.sys_id"));
},
type: 'DepartmentUtilsCustom'
});
Please mark my respsone as helpful/correct, if it answer your question.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2022 07:49 AM
So just place this in reference qualifier
javascript:new getChoices().getChoiceForNonHR();
Create a script include as I mentioned on above answer and give this filter of hr Core case and element and inactive in the script include add queries

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2022 08:14 AM
Within the script include as you suggested, how exactly would the filter be created, so that I can still get the lookup of choices?
Thanks,
-Rob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2022 08:18 AM
var getChoices = Class.create();
getChoices.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getChoiceForNonHR: function() {
var arr=[];
var dep = new GlideRecord('sys_user');
dep.addQuery('sys_id',gs.getUserID());
dep.query();
if (dep.next())
{
if(dep.department.getDisplayValue() == "Human Resources")// replace human resources dept name
{
var gr = new GlideRecord('sys_choice');
gr.addEncodedQuery('name=sn_hr_core_case_operations^element=u_elevated_wd_access_request_types^inactive=false^ORDERBYsequence');
gr.addEncodedQuery('label!=your_lable_name'); // add encoded query where label is not your required label name to be hidden
gr.query();
while(gr.next())
{
arr.push(gr.sys_id.toString());
}
}
}
return "sys_idIN"+arr;
},
type: 'getChoices'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2022 08:25 AM
If I do it that way, and turn the field into a reference field to the sys_choice table, I lose the choices and now I am making the user have to type in what they are looking for, and I cannot do that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2022 07:46 AM
Hello Rob,
If your requirement is to use a drop down field then I would suggest not to use the reference type field. You can use the Select box type (choice type) field which is a best practice, because if you have a requirement to create UI policy on this field or Hide other fields based on this field value if future, then it will be difficult for you or team to implement the UI Policy because the reference qualifier will not be applicable on condition field in UI policy table. So you we end up having 100s of value for your field when creating UI Policy.
If you are using the Select box type field then you can use the below code:
function onLoad() {
var ga = new GlideAjax("DepartmentUtilsCustom");
ga.addParam('sysparm_name', 'isUserFromHrDepartment');
ga.getXMLAnswer(getResources);
function getResources(response) {
var answer = response;
alert(answer);
if (!answer || answer == "false") {
var removeOptions = ["value1", "value2", "value3"]; // you can add the drop-down choice field value that you want to remove
for (var optionValues in removeOptions) {
g_form.removeOption("<YOUR_FIELD_NAME>", removeOptions[optionValues]);
}
}
}
}
Create a System property:
Name: hr.department.sys_id
Value: <sys_id of human resource department in your instance>
Script include:
var DepartmentUtilsCustom = Class.create();
DepartmentUtilsCustom.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isUserFromHrDepartment: function () {
// As the department of logged in user will not change frequently we can get the Department ID of logged user from user's current session
return (gs.getUser().getDepartmentID() == gs.getProperty("hr.department.sys_id"));
},
type: 'DepartmentUtilsCustom'
});
Please mark my respsone as helpful/correct, if it answer your question.
Thanks