What is the best way to hide a related list by user role?

Marcel H_
Tera Guru

I have a requirement to only display a related list on a form when the logged in user has a specific role. I've tried to do the following in a UI Policy as well as an onLoad Client Script, but the list always appears even if the current user doesn't have the specified role.

Client Script:

function onLoad() {
	if (g_user.hasRole('cat_man')) {
		g_form.showRelatedList('REL:dfb2edae1b50e51069fecddf034bcbc7');	
	}
	
	else {
		g_form.hideRelatedList('REL:dfb2edae1b50e51069fecddf034bcbc7');
	}
}

 

UI Policy Script:

//execute if true
function onCondition() {
	if (g_user.hasRole('cat_man')) {
		g_form.showRelatedList('REL:dfb2edae1b50e51069fecddf034bcbc7');	
	}


//execute if false
function onCondition() {
g_form.hideRelatedList('REL:dfb2edae1b50e51069fecddf034bcbc7');
}

 

The goal of course is to only allow specifically roled users to see the related list, and I will also need to do the same with some form sections. What is the best way to do this?

1 ACCEPTED SOLUTION

Basheer
Mega Sage

Hi @Marcel H_ ,

It seems you've coded it correctly

I would suggest to go for alerts to check whether the code is working perfectly or not

If the alerts are not entering then go for g_user.hasRoleExactly('cat_man')

function onLoad() {
alert("Triggered onload Client Script");
	if (g_user.hasRole('cat_man')) {
alert("satisfied the role condition");
		g_form.showRelatedList('REL:dfb2edae1b50e51069fecddf034bcbc7');	
	}
	
	else {
alert("did not satisfied the role condition");
		g_form.hideRelatedList('REL:dfb2edae1b50e51069fecddf034bcbc7');
	}
}

Let me know if you are getting the results via hasRole or hasRoleExactly in the correct way. Will try to resolve from there.

 

Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.

View solution in original post

4 REPLIES 4

Basheer
Mega Sage

Hi @Marcel H_ ,

It seems you've coded it correctly

I would suggest to go for alerts to check whether the code is working perfectly or not

If the alerts are not entering then go for g_user.hasRoleExactly('cat_man')

function onLoad() {
alert("Triggered onload Client Script");
	if (g_user.hasRole('cat_man')) {
alert("satisfied the role condition");
		g_form.showRelatedList('REL:dfb2edae1b50e51069fecddf034bcbc7');	
	}
	
	else {
alert("did not satisfied the role condition");
		g_form.hideRelatedList('REL:dfb2edae1b50e51069fecddf034bcbc7');
	}
}

Let me know if you are getting the results via hasRole or hasRoleExactly in the correct way. Will try to resolve from there.

 

Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.

If relying on inherited roles, you could try to do the opposite, block those that have a higher role.  g_user.hasRole('cat_man') == true   &&  !g_user.hasRole('admin');

Using hasRoleExactly() did the trick, and also worked for hiding a form section using the same role criteria. Thanks!

Kirby R
Kilo Sage

I tried using g_form.showRelatedList('task_sla'); on my instance and it seemed to work. g_form.showRelatedList(<table name of the related list>);