Restrict "Assigned to" changes to only group members.

gracjan
Tera Guru

So the goal is to permit only group members to "assigned to" tasks among themselves and not the customer. For instance, when a group is assigned and if a user is not part of the assignment group, they get read-only field for "Assigned to", when the user who access the form is part of the group they get the choice to pick the group member. Please help.

 

sn.PNG

1 ACCEPTED SOLUTION

Ok. I am answering my own question. I tried dark megic and it worked.

This is script include:

var UserInfo4 = Class.create();
UserInfo4.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isUserInGroup: function() {
var user_id = this.getParameter('sysparm_user_id');
var group_id = this.getParameter('sysparam_group_id');

var grUser = new GlideRecord('sys_user_grmember');
grUser.addQuery('user', user_id);
grUser.addQuery('group', group_id);
grUser.query();
if (grUser.next()) {
return "true";
}
return 'false';
},
type: 'UserInfo4'
});

This is OnChange client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
var user_id = g_user.userID;

   var ajax = new GlideAjax('UserInfo4');
    ajax.addParam('sysparm_name', 'isUserInGroup');
    ajax.addParam('sysparm_user_id', user_id);
    ajax.addParam('sysparam_group_id', newValue);
    ajax.getXML(setisUserInGroup);
        
 	function setisUserInGroup(response) {
 		var answer = response.responseXML.documentElement.getAttribute('answer');
		//alert(answer);
 	if(answer == "true"){g_form.setReadOnly('assigned_to', false);}else{g_form.setReadOnly('assigned_to', true);														  
   }
    }
	
	
}

This is onLoad:

function onLoad() {
   //Type appropriate comment here, and begin script below
	//alert(g_scratchpad.grp);
	
	
var user_id = g_user.userID;
	var group_id = g_form.getValue('assignment_group')

   var ajax = new GlideAjax('UserInfo4');
    ajax.addParam('sysparm_name', 'isUserInGroup');
    ajax.addParam('sysparm_user_id', user_id);
    ajax.addParam('sysparam_group_id', group_id);
    ajax.getXML(setisUserInGroup);
        
 	function setisUserInGroup(response) {
 		var answer = response.responseXML.documentElement.getAttribute('answer');
		//alert(answer);
 	if(answer == "true"){g_form.setReadOnly('assigned_to', false);}else{g_form.setReadOnly('assigned_to', true);														  
   }
    }
}

View solution in original post

6 REPLIES 6

DrewW
Mega Sage
Mega Sage

I see two ways to go about this

1 - Add an ACL to the assigned to field for the table that uses a script and checks if they are a member of the group and if not then return false so the field is read only.

https://developer.servicenow.com/dev.do#!/reference/api/sandiego/server_legacy/GUserAPI#GUser-isMemb...

 

2 - User a onLoad client script to make a client GlideRecord/REST API call to check the sys_user_grmember table to see if the current user is in the group and if not make the field read-only.

https://developer.servicenow.com/dev.do#!/reference/api/sandiego/client/c_GlideFormAPI#r_GlideFormSe...

 

I would suggest option 1, it will be easier and will prevent the person from using the list to change the assigned to.

 

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi,
You need to write a onLoad client script for this with a GlideAjax (to check if user is a part of assignment group or not).

GlideAjax | ServiceNow Developers

 

 


Thanks and Regards,

Saurabh Gupta

GlideAjax is a dark magic to me. Would you mind pointing me how could I achieve this?

Ok. I am answering my own question. I tried dark megic and it worked.

This is script include:

var UserInfo4 = Class.create();
UserInfo4.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isUserInGroup: function() {
var user_id = this.getParameter('sysparm_user_id');
var group_id = this.getParameter('sysparam_group_id');

var grUser = new GlideRecord('sys_user_grmember');
grUser.addQuery('user', user_id);
grUser.addQuery('group', group_id);
grUser.query();
if (grUser.next()) {
return "true";
}
return 'false';
},
type: 'UserInfo4'
});

This is OnChange client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
var user_id = g_user.userID;

   var ajax = new GlideAjax('UserInfo4');
    ajax.addParam('sysparm_name', 'isUserInGroup');
    ajax.addParam('sysparm_user_id', user_id);
    ajax.addParam('sysparam_group_id', newValue);
    ajax.getXML(setisUserInGroup);
        
 	function setisUserInGroup(response) {
 		var answer = response.responseXML.documentElement.getAttribute('answer');
		//alert(answer);
 	if(answer == "true"){g_form.setReadOnly('assigned_to', false);}else{g_form.setReadOnly('assigned_to', true);														  
   }
    }
	
	
}

This is onLoad:

function onLoad() {
   //Type appropriate comment here, and begin script below
	//alert(g_scratchpad.grp);
	
	
var user_id = g_user.userID;
	var group_id = g_form.getValue('assignment_group')

   var ajax = new GlideAjax('UserInfo4');
    ajax.addParam('sysparm_name', 'isUserInGroup');
    ajax.addParam('sysparm_user_id', user_id);
    ajax.addParam('sysparam_group_id', group_id);
    ajax.getXML(setisUserInGroup);
        
 	function setisUserInGroup(response) {
 		var answer = response.responseXML.documentElement.getAttribute('answer');
		//alert(answer);
 	if(answer == "true"){g_form.setReadOnly('assigned_to', false);}else{g_form.setReadOnly('assigned_to', true);														  
   }
    }
}