- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-15-2017 07:54 PM
Hi guys,
I have a Client script onLoad which removes an "approval" action from a drop down list when the logged in user is not part of a site's manager group. The script works perfectly fine when I add/remove myself from this group, however when I am adding users to that group the script is not working for them.
I read in the wiki that dot walking doesn't work in client scripts, however that doesn't explain why my alerts are printing the dot-walked fields correctly, nor why the code works when I add and remove myself from that manager group (I can't see why an admin role would change any of this code).
var person = g_user.userID;
var managerGroup = g_form.getDisplayBox('location').value + " - Site Managers";
var mr = new GlideRecord('sys_user_group');
mr.addQuery('name', managerGroup);
mr.query();
if(mr.next()){
var ir = new GlideRecord('sys_user_grmember');
ir.addQuery('group', mr.sys_id);
ir.addQuery('user', person);
ir.query();
alert("manager group: "+ mr.name + ", manager sys_id " + mr.sys_id);
if(ir.next()){
alert(g_user.getFullName() + " is in the group")
// Show all options if it's 'Pending Extension'
// Otherwise use the below logic to show/hide the options.
if (g_form.getValue('state') != -35 ){
// Only show Approve if the state is Pending Client Approval.
if (g_form.getValue('state') == -31 ){
g_form.removeOption('u_action', 'Extend');
g_form.removeOption('u_action', 'complete');
} else{
g_form.removeOption('u_action', 'approve');
}
}
} else {
alert(g_user.getFullName() + " is NOT in the group");
g_form.removeOption('u_action', 'approve');
}
} else {
alert("no site manager group found");
}
Can anyone please guide me to a different way to show/hide items in a choice list like this, or can you identify a fault in the above code that i'm missing?
Solved! Go to Solution.
- Labels:
-
Incident Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2017 07:29 PM
Hi Ahmad,
Can you please give a try to run the client script with getXML() instead of getXMLAnswer().
--------------Client script--------------
function onLoad() {
var loc= g_form.getReference('location', doAlert); // doAlert is our callback function
}
function doAlert(loc) { //reference is passed into callback as first arguments
var grp = loc.name;
var gax = new GlideAjax('AL_checkUserGroup');
gax.addParam('sysparm_name','isUserSiteManager');
gax.addParam('sysparm_location',grp);
gax.getXML(ajaxResponse);
function ajaxResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
if(answer == true) {
//code
else {
//more code
}
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2017 12:10 AM
Hi Ahmad,
Is your location field read only as getDisplayBox does not work when the field is read only (by an ACL).
Please validate
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2017 12:28 AM
Hi Sneha,
The location field is read only, however for testing purposes I unchecked the read-only property and it is still not working.
SO to summarise what is happening right now, I am running the glideRecord with NO queries, and the next() function is still returning false for the other user, but true for me.
var gr = new GlideRecord('sys_user_grmember');
gr.query(getResults);
function getResults(result) {
if (result.next()) {
alert(g_user.userName + ' is in the group');
console.log(result);
} else {
alert(g_user.userName + ' is NOT in the group');
console.log(result);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2017 12:52 AM
I have made some progress in troubleshooting, when the user has itil or admin role, the above statement evaluates to true. However, if I take the roles out then the statement evaluates to false.
Since the person can't see the sys_user_grmember table does that mean that this "restriction" translates into this GlideRecord query and denies the client script from accessing these records too? If that is true, then this is going to be very painful going forward, as we would then need to give users unnecessary permissions just for a check (or i'll have to implement this another way, but I wonder if i'm going to face the same issue in an ajax/script include though...)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2017 01:20 AM
Hi Ahmad,
No the restriction will not deny client script for accessing the record.
Try writing your code in Client callable script include and pass in the details of the record in the way mentioned below.
var loc= g_form.getReference('location', doAlert); // doAlert is our callback function
//Type appropriate comment here, and begin script below
}
function doAlert(loc) { //reference is passed into callback as first arguments
var grp = loc.name;
var gax = new GlideAjax('Script include name');
gax.addParam('sysparm_name','function_name');
gax.addParam('sysparm_location',grp);
gax.getXMLAnswer(function(answer) {
if(answer == "true") {
// do whatever you want to do
}
else {
//do whatever action you want to perform.
}
});
}
Now in the script include place in your code to fetch the group member details and return true or false.
Hope this helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2017 06:25 PM
Hi Sneha,
Thanks for the help, ajax/script include is looking like a promising solution but I think I have written something incorrectly, can you (or someone else) please tell me what i'm doing wrong? I'm not calling the function correctly because nothing is being run in the client script. I don't have much experience yet with how script includes work yet unfortunately.
--------------Client script--------------
function onLoad() {
var loc= g_form.getReference('location', doAlert); // doAlert is our callback function
}
function doAlert(loc) { //reference is passed into callback as first arguments
var grp = loc.name;
var gax = new GlideAjax('AL_checkUserGroup');
gax.addParam('sysparm_name','isUserSiteManager');
gax.addParam('sysparm_location',grp);
gax.getXMLAnswer(function(answer) {
alert("answer: " + answer); //*****ANSWER AT THIS POINT IS NULL**********
if(answer == "true") {
//code
else {
//more code
}
});
}
--------------Script Include--------------
var AL_checkUserGroup = Class.create();
AL_checkUserGroup.prototype = {
initialize: function() {
},
isUserSiteManager: function(){
var managerGroup = this.getParameter('sysparm_location') + " - Site Managers";
var mr = new GlideRecord('sys_user_group');
mr.addQuery('name', managerGroup);
mr.query();
if(mr.next()){
var ir = new GlideRecord('sys_user_grmember');
ir.addQuery('group', mr.sys_id);
ir.addQuery('user', person);
ir.query();
if(ir.next()){
return true;
}
else {
return false;
}
} else {
return false;
}
},
type: 'AL_checkUserGroup'
};