- 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 06:23 AM
Hi Rob,
You can use the following code to hide the field choices based on the loggedIn User department:-
Client Script
function onLoad() {
//Type appropriate comment here, and begin script below
var userID = g_user.userID;
//alert(userID);
var ga = new GlideAjax("demoTestingSI");
ga.addParam('sysparm_name', 'getDetails');
ga.addParam('sysparm_caller', userID);
// alert("kunal");
ga.getXML(getResources);
function getResources(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}
}
Script Include:-
var demoTestingSI = Class.create();
demoTestingSI.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDetails: function() {
var abc = this.getParameter('sysparm_caller');
var grp = new GlideRecord('sys_user');
grp.addQuery('sys_id', abc);
grp.query();
if (grp.next()) {
// gs.addInfoMessage("the email is");
//gs.error("2.0 error");
var av = grp.department.getDisplayValue();
return av;
}
},
type: 'demoTestingSI'
});
Kindly mark this as helpful is this solves your issue.
Thank you,
Kunal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2022 06:37 AM
Hey Kunal,
Thank you for responding - however, are you able to assist a bit more?
Out of all my choices (37 of them), I need to hide certain ones. Where would I be doing that in the script you have provided?
Also, where are we saying, "user is not in human resources department"?
If you can provide additional details with these scripts that would be great.
Thank you,
-Rob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2022 06:45 AM
Hi,
Here is the complete solution:-
function onLoad() {
//Type appropriate comment here, and begin script below
var userID = g_user.userID;
var ga = new GlideAjax("demoTestingSI");
ga.addParam('sysparm_name', 'getDetails');
ga.addParam('sysparm_caller', userID);
ga.getXML(getResources);
function getResources(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
if(answer=='a581ab703710200044e0bfc8bcbe5de8'){ //paste your department sys Id here after the == operator
g_form.removeOption('category','inquiry');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2022 06:57 AM
Hello
if you are using a reference field which is referring to sys_choice table you need a reference qualifier and a non client callable script include for this .No need of client script
Use this advanced reference qualifier in reference field where you are referring to sys_chocie table
javascript:new getChoices().getChoiceForNonHR();
Script include:
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.addQuery('element','your_element_name');// replace your element name
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'
});
Hope this helps
please mark my answer correct if this helps you