- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2023 07:04 PM
Hi All,
I have to restrict users with sn_customerservice.consumer role to not access few categories under "Request" record producer. Is there a way this can be done.
Detailed information:
In "Request" record producer I have two drop downs category and subcategory.
I need the options on this drop down to be limited based on the user role/group.
For Ex: Category has below options
1) B&A studio
2) My IT
3) My office equipment
My requirement is when users belonging to a specific group/role try to create a case for this record producer then they should only see one option in category dropdown that is B&A studio.
Can this be done using client script?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2023 09:53 PM
Hi @harshamovva ,
Look at these example Client Callable Script Include and onLoad Catalog Client Script. Here, I'm trying to populate the B&A Studio option if the user is part of Hardware group or having the role ITIL. The remaining two options will be accessible to all other users, so included them in the question choices itself.
Variable:
Script Include:
var CatalogUserData = Class.create();
CatalogUserData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isMemberOf: function(){
var group = this.getParameter('sysparm_group');
var data = {
'isMember' : gs.getUser().isMemberOf(group)
};
return JSON.stringify(data);
},
isHavingRole: function(){
var role = this.getParameter('sysparm_role');
var roleGr = new GlideRecord('sys_user_has_role');
roleGr.addEncodedQuery('role.name='+role+'^user=' + gs.getUserID());
roleGr.query();
var isHavingRole = false;
if(roleGr.next()){
isHavingRole = true;
}
var data = {
'isHavingRole' : gs.getUser().hasRole('itil') || isHavingRole
};
return JSON.stringify(data);
},
type: 'CatalogUserData'
});
Note: I wrote some redundant code for checking role, that is intended.
onLoad Client Script:
function onLoad() {
//Check if the user is part of a group
var usgGa = new GlideAjax('CatalogUserData');
usgGa.addParam('sysparm_name', 'isMemberOf');
usgGa.addParam('sysparm_group', 'Hardware');
usgGa.getXMLAnswer(populateOptionGroup);
function populateOptionGroup(answer){
alert(answer);
if(answer){
alert(1);
var data = JSON.parse(answer);
alert(data['isMember']);
if(data['isMember'] == true){
alert(2);
g_form.addOption('category', 'B&A_studio', 'B&A studio');
}
}
}
//Check if the user is having a role
var usrGa = new GlideAjax('CatalogUserData');
usrGa.addParam('sysparm_name', 'isHavingRole');
usrGa.addParam('sysparm_group', 'itil');
usrGa.getXMLAnswer(populateOptionRole);
function populateOptionRole(answer){
alert('Role2');
if(answer){
alert('Role1');
var data = JSON.parse(answer);
alert(answer);
if(data['isHavingRole'] == true){
alert('Role');
g_form.addOption('category', 'B&A_studio', 'B&A studio');
}
}
}
}
Thanks,
Anvesh
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2023 07:29 PM
Hi @harshamovva,
You can do this using a Client Callable Script Include and Calling that using GlideAjax in Client Script to check does the user have the required role or not. Check this answer for reference https://www.servicenow.com/community/developer-forum/hiding-fields-on-a-catalog-item-form-if-user-is...
If the user has required role, then we can use g_form.addOption(fieldName, value, label, index) to add the choices to the drop-down. In the above answer we are hiding a field based on the user role, in the same manner you can add option or remove option from the drop-down.
This way we can ensure the user will see the Choices if he has the role required.
Let me know if you need any more help on this.
Thanks,
Anvesh
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2023 07:49 PM
Thank you @AnveshKumar M . Will try this out.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2023 09:53 PM
Hi @harshamovva ,
Look at these example Client Callable Script Include and onLoad Catalog Client Script. Here, I'm trying to populate the B&A Studio option if the user is part of Hardware group or having the role ITIL. The remaining two options will be accessible to all other users, so included them in the question choices itself.
Variable:
Script Include:
var CatalogUserData = Class.create();
CatalogUserData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isMemberOf: function(){
var group = this.getParameter('sysparm_group');
var data = {
'isMember' : gs.getUser().isMemberOf(group)
};
return JSON.stringify(data);
},
isHavingRole: function(){
var role = this.getParameter('sysparm_role');
var roleGr = new GlideRecord('sys_user_has_role');
roleGr.addEncodedQuery('role.name='+role+'^user=' + gs.getUserID());
roleGr.query();
var isHavingRole = false;
if(roleGr.next()){
isHavingRole = true;
}
var data = {
'isHavingRole' : gs.getUser().hasRole('itil') || isHavingRole
};
return JSON.stringify(data);
},
type: 'CatalogUserData'
});
Note: I wrote some redundant code for checking role, that is intended.
onLoad Client Script:
function onLoad() {
//Check if the user is part of a group
var usgGa = new GlideAjax('CatalogUserData');
usgGa.addParam('sysparm_name', 'isMemberOf');
usgGa.addParam('sysparm_group', 'Hardware');
usgGa.getXMLAnswer(populateOptionGroup);
function populateOptionGroup(answer){
alert(answer);
if(answer){
alert(1);
var data = JSON.parse(answer);
alert(data['isMember']);
if(data['isMember'] == true){
alert(2);
g_form.addOption('category', 'B&A_studio', 'B&A studio');
}
}
}
//Check if the user is having a role
var usrGa = new GlideAjax('CatalogUserData');
usrGa.addParam('sysparm_name', 'isHavingRole');
usrGa.addParam('sysparm_group', 'itil');
usrGa.getXMLAnswer(populateOptionRole);
function populateOptionRole(answer){
alert('Role2');
if(answer){
alert('Role1');
var data = JSON.parse(answer);
alert(answer);
if(data['isHavingRole'] == true){
alert('Role');
g_form.addOption('category', 'B&A_studio', 'B&A studio');
}
}
}
}
Thanks,
Anvesh
Anvesh