- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2020 02:18 PM
Hi Community, I'm working on a catalog item request with the below requirements. My client script isn't working in the Service Portal and I believe it's because I need to use GlideAjax instead of GlideRecord. I'm on a deadline, so any input would be greatly appreciated. Thank you.
- Show 'Assigned To' variable if logged in user is a member of 'Facilities' group
- Show 'Close Notes' variable if logged in user is a member of 'Facilities' group
- Hide Assigned To and Close Notes variables if logged in user isn't member of Facilities group
onLoad Client Script:
function onLoad() {
var userID = g_user.userID;
if (GetGroupMembers(userID) == true){
g_form.setDisplay('variables.assigned_to', true);
g_form.setDisplay('variables.close_notes', true);
}
else{
g_form.setDisplay('variables.assigned_to',false);
g_form.setDisplay('variables.close_notes', false);
}
}
function GetGroupMembers(user) {
var usrGrp=new GlideRecord("sys_user_grmember");
usrGrp.addQuery('group', '9682088ddb230010df87ab7dca9619cd');
usrGrp.addQuery('user', user);
usrGrp.query();
if(usrGrp.next()){
return true;
}
else {
return false;
}
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2020 03:29 AM
Hi,
Okay so this is for catalog client script and on the catalog item view while submitting the request; in this case display business rule won't work;
you would require to create script include and use GlideAjax in onload 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', 'Facilities');
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('assigned_to', true);
g_form.setDisplay('close_notes', true);
}
else{
g_form.setDisplay('assigned_to',false);
g_form.setDisplay('close_notes', false);
}
});
}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2020 06:14 AM
Let alone the questions of performance and good practice, client-side GlideRecord is executed in the security context of the current user. With your initial approach, the results of your GlideRecord query depend on whether or not the current user has access to read from Group Members [sys_user_grmember] table.
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/