Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Conditions for UI Action for being displayed

Alon Grod
Tera Expert

I need to create a new UI Action on the incident form (a button). The button needs to be displayed only when these conditions are met:
1) the login user is 'admin'
OR

2. the login user is a member of the current assignment group
OR

3. the login user  is a member of the parent's group of the current assignment group.

I tried to use these conditions:

gs.hasRole('admin') || gs.getUser().isMemberOf(current.assignment_group.name) || gs.getUser().isMemberOf(current.assignment_group.parent.name)

 

The problem is that Im able to see the UI Action whenever the assignment group is my group's parent and whenever the assignment group has the same group's parent like I have.

For example,
If i belong to 'Software' group and my group's parent is 'Computers'. When the assignment group on the incident is 'Computers' Im not supposed to see the UI Action (but right now I do see). In addition, if the assignment group is 'Hardware' and we share the same group's parent ('Computers'), Im not suppose to see the UI Action. But if I belong to 'Computers' i suppose to see the UI Action whenever the assignment group is on of my Childs or the group itself.

What do I need to change?

1 ACCEPTED SOLUTION

Clara Lemos
Mega Sage

Hi @Alon Grod ,

 

I would recommend using a script include for this 

You can add it to your UI Action condition for example, javascript: new IsMemberOf().CheckMembership(); 

 

And your script include could be something like this:

 

 

 

var IsMemberOf = Class.create();
IsMemberOf.prototype = {
    initialize: function() {},

	CheckMembership: function(){

		if(gs.hasRole('admin')){ 
			return true;
		}
		var user = gs.getUserID(); 
		var member = new GlideRecord('sys_user_grmember');
		member.addQuery('user',user);
		member.query();
		while(member.next()){ 

			if(member.group == current.assignment_group){
				return true;
			}
			if(member.group == current.assignment_group.parent){
				return true;
			}
		}
		return false;
		
	},

    type: 'IsMemberOf'
};

 

 

 If that works for you please mark my answer as correct/ helpful šŸ™‚ 

Let me know if can I help you further 

 

Cheers 

View solution in original post

3 REPLIES 3

Samaksh Wani
Giga Sage

Hello @Alon Grod 

 

Try using the AND Operator between 2nd and 3rd Operator.

 

gs.hasRole('admin') || gs.getUser().isMemberOf(current.assignment_group.name) && gs.getUser().isMemberOf(current.assignment_group.parent.name)

 

Plz mark my solution as Accept, If you find it helpful.

 

Regards,

Samaksh

@Samaksh Wani still not working

Clara Lemos
Mega Sage

Hi @Alon Grod ,

 

I would recommend using a script include for this 

You can add it to your UI Action condition for example, javascript: new IsMemberOf().CheckMembership(); 

 

And your script include could be something like this:

 

 

 

var IsMemberOf = Class.create();
IsMemberOf.prototype = {
    initialize: function() {},

	CheckMembership: function(){

		if(gs.hasRole('admin')){ 
			return true;
		}
		var user = gs.getUserID(); 
		var member = new GlideRecord('sys_user_grmember');
		member.addQuery('user',user);
		member.query();
		while(member.next()){ 

			if(member.group == current.assignment_group){
				return true;
			}
			if(member.group == current.assignment_group.parent){
				return true;
			}
		}
		return false;
		
	},

    type: 'IsMemberOf'
};

 

 

 If that works for you please mark my answer as correct/ helpful šŸ™‚ 

Let me know if can I help you further 

 

Cheers