How can i set field visibility based on role?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2025 03:17 AM
Hello
How can i control field visibility on a form based on role.
Suppose i have two role "ROLE_A" and "ROLE_B" and my requirement is for ROLE_A all the fields on incident form should be visible except description and for ROLE_B only description field should be visible and rest field should be hidden.
How can i achieve this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2025 03:54 AM
Hi @Ankit Kumar6 ,
You can create an OnLoad Client script and use g_user.hasRoleExactly() and g_form.setVisble() API
if(g_form.hasRoleExactly("ROLE_A")
{
g_form.setVisible("description",false); // hide the field for user having ROLE_A
}
if(g_form.hasRoleExactly("ROLE_B")
{
g_form.setVisible("field_name",false); // hide the field for user having ROLE_B
g_form.setVisible("field_name_1",false);
g_form.setVisible("field_name_2",false);
g_form.setVisible("field_name_3",false);
}
If my response helped, please hit the Thumb Icon and accept the solution so that it benefits future readers.
Regards,
Rohit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2025 04:07 AM
Hello @Rohit Singh
for the above proposed solution i have to write multiple "g_form.setVisible("field_name_1",false);"
is there any other way or how can i do it via ACL?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2025 04:09 AM
Hello @Ankit Kumar6
I don't think we should be using hasroleexactly - because it means - person should be granted this role directly and not even inherited roles it will consider. For eg: - Role A is within admin role then admin will not be granted access - because role is granted by inheritance.
Try below -
function onLoad() {
// Define roles
var roleA = 'ROLE_A';
var roleB = 'ROLE_B';
// Define fields
var allFields = ['short_description', 'priority', 'category', 'impact', 'urgency']; // Add other fields as needed
var descriptionField = 'description';
// Check if user has ROLE_A
if (g_user.hasRole(roleA)) {
g_form.setDisplay(descriptionField, false); // Hide Description
allFields.forEach(function(field) {
g_form.setDisplay(field, true); // Show all other fields
});
}
// Check if user has ROLE_B
else if (g_user.hasRole(roleB)) {
g_form.setDisplay(descriptionField, true); // Show Description
allFields.forEach(function(field) {
g_form.setDisplay(field, false); // Hide all other fields
});
}
}
Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket.
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOw
NeEISQCY