Inactivate the attachment icon once the change is inactive

DeIvory Gordon
Tera Guru

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?

 

DeIvoryGordon_0-1749491689922.png

 

1 ACCEPTED SOLUTION

Bert_c1
Kilo Patron

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:

Screenshot 2025-06-09 170004.png

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.

View solution in original post

4 REPLIES 4

Bert_c1
Kilo Patron

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

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.

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.

Bert_c1
Kilo Patron

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:

Screenshot 2025-06-09 170004.png

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.