Hide a custom journal field

Afshin_Lakzadeh
Tera Contributor

We have created a custom journal field, but we want the field to only be available to users who are member of a specific group. We have tried to hide the field via Client Script and Scripted UI Policy without success.
We have also tried with UI Policy, and the field gets hidden for the assignment group, but we cannot check if the user is a member of the group.
What is the best practice for hiding journal fields, and what would you recommend?

2 ACCEPTED SOLUTIONS

Anand Kumar P
Giga Patron
Giga Patron

Hi @Afshin_Lakzadeh ,

 

To make a custom journal field visible only to certain group members in ServiceNow, it’s best to use a combination of a Client Script and a Script Include, which checks group membership on the server

 

Create a Script Include:

This Script Include will check if the user is in the specified group. Ensure it’s set up to be called by the Client Script.

 

Script Include Below:

var u_userInfoAjax = Class.create();
u_userInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getGroupMembership: function(){
		var user = gs.getUserID();
		var gr = new GlideRecord("sys_user_grmember");
		gr.addQuery('group.name', 'specific group name');
                gr.addQuery('user', user);
                gr.query();
                return gr.hasNext();
		
	},

	type: 'u_userInfoAjax'
});

Client Script:

function onLoad(){

var ga = new GlideAjax('u_userInfoAjax');
ga.addParam('sysparm_name', "getGroupMembership");
ga.getXMLAnswer(function(answer){

if(answer.toString() == 'true'){
g_form.setDisplay('journal_field_backendname', true);
}

else{
g_form.setDisplay('journal_field_backendname', false);
}

});

}

 Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand

View solution in original post

Hello @Afshin_Lakzadeh ,

Yes, we can make the Script Include more dynamic by allowing the group name to be passed in from the Client Script. Below are the adjustments you can make to the existing Script Include and Client Script:

Updated Script Include:

var u_userInfoAjax = Class.create();
u_userInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getGroupMembership: function(){
		var user = gs.getUserID();
		var groupName = this.getParameter('sysparm_group_name'); // Get the group name from the parameter
		var gr = new GlideRecord("sys_user_grmember");
		gr.addQuery('group.name', groupName);
        gr.addQuery('user', user);
        gr.query();
        return gr.hasNext();
	},

	type: 'u_userInfoAjax'
});

 

Updated Client Script:

function onLoad(){
	var ga = new GlideAjax('u_userInfoAjax');
	ga.addParam('sysparm_name', "getGroupMembership");
	ga.addParam('sysparm_group_name', "Your Group Name Here"); // Set the group name dynamically
	ga.getXMLAnswer(function(answer){
		if(answer.toString() == 'true'){
			g_form.setDisplay('journal_field_backendname', true);
		} else {
			g_form.setDisplay('journal_field_backendname', false);
		}
	});
}


Please give it a try and see how it works for you and let me know if you face any issues anywhere.

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.


Regards,
Aniket

View solution in original post

6 REPLIES 6

You can use @Aniket Chavan script it will work for dynamic groups.

 

Thanks,

Anand

Runjay Patel
Giga Sage

Hi @Afshin_Lakzadeh ,

 

create one display business rule and set the scratchpad variable true and false.

in your BR you can write like below.

g_scratchpad.flag=(gs.getUser().isMemberOf(‘your group name’)

now write one on load client script and do the following.


if(g_scratchpad.flag)

hide filed 

else

show field