- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2024 12:29 AM
We have created a custom journal field, but we want the field to only be available to users who are member of a specific group. We have tried to hide the field via Client Script and Scripted UI Policy without success.
We have also tried with UI Policy, and the field gets hidden for the assignment group, but we cannot check if the user is a member of the group.
What is the best practice for hiding journal fields, and what would you recommend?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2024 12:53 AM - edited 10-29-2024 12:57 AM
Hi @Afshin_Lakzadeh ,
To make a custom journal field visible only to certain group members in ServiceNow, it’s best to use a combination of a Client Script and a Script Include, which checks group membership on the server
Create a Script Include:
This Script Include will check if the user is in the specified group. Ensure it’s set up to be called by the Client Script.
Script Include Below:
var u_userInfoAjax = Class.create();
u_userInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getGroupMembership: function(){
var user = gs.getUserID();
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery('group.name', 'specific group name');
gr.addQuery('user', user);
gr.query();
return gr.hasNext();
},
type: 'u_userInfoAjax'
});
Client Script:
function onLoad(){
var ga = new GlideAjax('u_userInfoAjax');
ga.addParam('sysparm_name', "getGroupMembership");
ga.getXMLAnswer(function(answer){
if(answer.toString() == 'true'){
g_form.setDisplay('journal_field_backendname', true);
}
else{
g_form.setDisplay('journal_field_backendname', false);
}
});
}
Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2024 04:27 AM
Hello @Afshin_Lakzadeh ,
Yes, we can make the Script Include more dynamic by allowing the group name to be passed in from the Client Script. Below are the adjustments you can make to the existing Script Include and Client Script:
Updated Script Include:
var u_userInfoAjax = Class.create();
u_userInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getGroupMembership: function(){
var user = gs.getUserID();
var groupName = this.getParameter('sysparm_group_name'); // Get the group name from the parameter
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery('group.name', groupName);
gr.addQuery('user', user);
gr.query();
return gr.hasNext();
},
type: 'u_userInfoAjax'
});
Updated Client Script:
function onLoad(){
var ga = new GlideAjax('u_userInfoAjax');
ga.addParam('sysparm_name', "getGroupMembership");
ga.addParam('sysparm_group_name', "Your Group Name Here"); // Set the group name dynamically
ga.getXMLAnswer(function(answer){
if(answer.toString() == 'true'){
g_form.setDisplay('journal_field_backendname', true);
} else {
g_form.setDisplay('journal_field_backendname', false);
}
});
}
Please give it a try and see how it works for you and let me know if you face any issues anywhere.
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
10-29-2024 12:53 AM - edited 10-29-2024 12:57 AM
Hi @Afshin_Lakzadeh ,
To make a custom journal field visible only to certain group members in ServiceNow, it’s best to use a combination of a Client Script and a Script Include, which checks group membership on the server
Create a Script Include:
This Script Include will check if the user is in the specified group. Ensure it’s set up to be called by the Client Script.
Script Include Below:
var u_userInfoAjax = Class.create();
u_userInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getGroupMembership: function(){
var user = gs.getUserID();
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery('group.name', 'specific group name');
gr.addQuery('user', user);
gr.query();
return gr.hasNext();
},
type: 'u_userInfoAjax'
});
Client Script:
function onLoad(){
var ga = new GlideAjax('u_userInfoAjax');
ga.addParam('sysparm_name', "getGroupMembership");
ga.getXMLAnswer(function(answer){
if(answer.toString() == 'true'){
g_form.setDisplay('journal_field_backendname', true);
}
else{
g_form.setDisplay('journal_field_backendname', false);
}
});
}
Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2024 03:40 AM
Thanks. That works fine.
Can we make the Script Include more dynamic and set the group name in Client Script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2024 04:27 AM
Hello @Afshin_Lakzadeh ,
Yes, we can make the Script Include more dynamic by allowing the group name to be passed in from the Client Script. Below are the adjustments you can make to the existing Script Include and Client Script:
Updated Script Include:
var u_userInfoAjax = Class.create();
u_userInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getGroupMembership: function(){
var user = gs.getUserID();
var groupName = this.getParameter('sysparm_group_name'); // Get the group name from the parameter
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery('group.name', groupName);
gr.addQuery('user', user);
gr.query();
return gr.hasNext();
},
type: 'u_userInfoAjax'
});
Updated Client Script:
function onLoad(){
var ga = new GlideAjax('u_userInfoAjax');
ga.addParam('sysparm_name', "getGroupMembership");
ga.addParam('sysparm_group_name', "Your Group Name Here"); // Set the group name dynamically
ga.getXMLAnswer(function(answer){
if(answer.toString() == 'true'){
g_form.setDisplay('journal_field_backendname', true);
} else {
g_form.setDisplay('journal_field_backendname', false);
}
});
}
Please give it a try and see how it works for you and let me know if you face any issues anywhere.
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
10-29-2024 05:45 AM