- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2025 11:00 AM
Hello,
I need to inactivate the attachment icon on change requests for non Change Management Office (CMO) users when the is in any stat other than "New". I have to enable Change Management Office users to add an attachment at any state of the change. Here is the client script I used, it removes the attachment icon for non-CMO users AND CMO users. Do you know what is wrong with the script?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2025 02:02 PM
I've avoided the role check in the client script. the script there is now
function onLoad() {
//Type appropriate comment here, and begin script below
var chgState = g_form.getValue('state');
var ga = new GlideAjax('CheckUserRole');
ga.addParam('sysparm_name', 'checkRole');
ga.addParam('sysparm_user_id', g_user.userID);
ga.addParam('sysparm_role_id', '2831a114c611228501d4ea6c309d626d'); // sys_id of the desired role
ga.getXMLAnswer(getResponse, chgState);
// callback function for returning the result from the script include
function getResponse(response, chgSt) {
var rslt = response;
alert('The response: ' + response);
if ((rslt == 'false') && (chgSt != -5)) {
// alert('Hiding!');
g_form.disableAttachments();
}
}
}
A new script include is used for the user-role check:
script:
var CheckUserRole = Class.create();
CheckUserRole.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkRole: function() {
var userID = this.getParameter("sysparm_user_id");
var roleID = this.getParameter("sysparm_role_id");
// gs.info('checkUserRole: user = ' + userID + ', role = ' + roleID);
var retValue = false;
var suhr = new GlideRecord('sys_user_has_role');
suhr.addQuery('user', userID);
suhr.addQuery('role', roleID);
suhr.query();
if (suhr.next()) {
retValue = true;
}
// gs.info('checkUserRole: returning ' + retValue);
return retValue;
},
type: 'CheckUserRole'
});
I've tested, using the sys_id of the 'admin' role in the client script. Change that for your use case. and there is no attachment icon when impersonating Beth Anglin.
Maybe there's another way.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2025 11:19 AM - edited 06-09-2025 01:19 PM
Use the value for state: 'New', not the label. that is -5 in my PDI. as in:
function onLoad() {
//Type appropriate comment here, and begin script below
var chgState = g_form.getValue('state');
if ((chgState != -5) && (!g_user.hasRole('change_manager')) {
g_form.disableAttachments();
}
}
add your role check condition to the "if: statement
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2025 11:23 AM
The attachment icon hides when the state is not new and the user is a non cmo, but the attachment icon also hides when the user is a cmo.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2025 01:09 PM - edited 06-09-2025 01:45 PM
check sys_user_has_role table for the user-role combination. The above (edited to include the role check) but some other error appear now when I open a change_request.
After more testing, the role check is not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2025 02:02 PM
I've avoided the role check in the client script. the script there is now
function onLoad() {
//Type appropriate comment here, and begin script below
var chgState = g_form.getValue('state');
var ga = new GlideAjax('CheckUserRole');
ga.addParam('sysparm_name', 'checkRole');
ga.addParam('sysparm_user_id', g_user.userID);
ga.addParam('sysparm_role_id', '2831a114c611228501d4ea6c309d626d'); // sys_id of the desired role
ga.getXMLAnswer(getResponse, chgState);
// callback function for returning the result from the script include
function getResponse(response, chgSt) {
var rslt = response;
alert('The response: ' + response);
if ((rslt == 'false') && (chgSt != -5)) {
// alert('Hiding!');
g_form.disableAttachments();
}
}
}
A new script include is used for the user-role check:
script:
var CheckUserRole = Class.create();
CheckUserRole.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkRole: function() {
var userID = this.getParameter("sysparm_user_id");
var roleID = this.getParameter("sysparm_role_id");
// gs.info('checkUserRole: user = ' + userID + ', role = ' + roleID);
var retValue = false;
var suhr = new GlideRecord('sys_user_has_role');
suhr.addQuery('user', userID);
suhr.addQuery('role', roleID);
suhr.query();
if (suhr.next()) {
retValue = true;
}
// gs.info('checkUserRole: returning ' + retValue);
return retValue;
},
type: 'CheckUserRole'
});
I've tested, using the sys_id of the 'admin' role in the client script. Change that for your use case. and there is no attachment icon when impersonating Beth Anglin.
Maybe there's another way.