Role-based Section/Field Visibility
Show or hide sections or specific fields based on the current user's roles, useful for restricting sensitive information access.
onLoad Table: incident
#onLoad #roles #security #visibility #rbac #sections
Script Code
JavaScript
1function onLoad() {
2 // Configuration: Define visibility rules based on roles
3 // Key = role name, Value = object with sections and fields to show
4 var roleVisibilityMap = {
5 'itil': {
6 sections: ['closure_info', 'resolution_info'],
7 fields: ['close_code', 'close_notes', 'resolved_by', 'resolved_at']
8 },
9 'security_admin': {
10 sections: ['security_info'],
11 fields: ['u_security_classification', 'u_compliance_notes', 'u_data_breach']
12 },
13 'financial_user': {
14 sections: ['cost_info'],
15 fields: ['u_estimated_cost', 'u_actual_cost', 'u_billing_code']
16 }
17 };
18
19 // Get current user's roles
20 var userRoles = g_user.getRoles().split(',');
21
22 // Determine which sections/fields to show
23 var sectionsToShow = [];
24 var fieldsToShow = [];
25
26 // Check each role
27 userRoles.forEach(function(role) {
28 if (roleVisibilityMap[role]) {
29 // Add sections for this role
30 if (roleVisibilityMap[role].sections) {
31 sectionsToShow = sectionsToShow.concat(roleVisibilityMap[role].sections);
32 }
33 // Add fields for this role
34 if (roleVisibilityMap[role].fields) {
35 fieldsToShow = fieldsToShow.concat(roleVisibilityMap[role].fields);
36 }
37 }
38 });
39
40 // Hide all configured sections first
41 Object.keys(roleVisibilityMap).forEach(function(role) {
42 if (roleVisibilityMap[role].sections) {
43 roleVisibilityMap[role].sections.forEach(function(section) {
44 g_form.setSectionDisplay(section, false);
45 });
46 }
47 if (roleVisibilityMap[role].fields) {
48 roleVisibilityMap[role].fields.forEach(function(field) {
49 g_form.setDisplay(field, false);
50 });
51 }
52 });
53
54 // Show only sections/fields user has access to
55 sectionsToShow.forEach(function(section) {
56 g_form.setSectionDisplay(section, true);
57 });
58
59 fieldsToShow.forEach(function(field) {
60 g_form.setDisplay(field, true);
61 });
62
63 // Optional: Add info message if restricted content is hidden
64 if (sectionsToShow.length === 0 && fieldsToShow.length === 0) {
65 // Check if any restrictions apply
66 var hasRestrictions = Object.keys(roleVisibilityMap).length > 0;
67 if (hasRestrictions) {
68 g_form.addInfoMessage('Some fields are hidden based on your role permissions.');
69 }
70 }
71}
How to Use
1. Create an onLoad Client Script on your table
2. Customize the `roleVisibilityMap` object with your roles and fields/sections
3. Ensure section names match your form layout
4. Test with users having different role combinations
5. Consider adding sys_admin to bypass all restrictions if needed
6. Document which roles can see which sections for governance
Related Scripts
Auto-populate Related Fields
Automatically populate fields when a reference field changes (e.g., populate caller's phone and email when caller is selected).
Conditional Mandatory Fields
Make fields mandatory based on the value of another field (e.g., require close notes only when state is Resolved).
Dynamic Field Visibility
Show or hide fields based on another field's value (e.g., show "Other reason" field only when user selects "Other" from dropdown).