Showing a Section to Specific Users in ServiceNow
In this example, we will demonstrate how to show a specific section, called "Investigation," to only a particular group of users in ServiceNow.
Step 1: Create a Display Business Rule
First, create a display business rule that holds the scratchpad variable.
- Navigate to
System Definition > Business Rulesand click onNew. - Fill out the form with the necessary information:
- Name: Set a descriptive name, such as "Set Investigation Section Visibility".
- Table: Select the appropriate table.
- When: Set to
Display.
- Add a condition to ensure this business rule runs only when needed.
- In the
Scripttab, use the following script to set a scratchpad variable:(function executeRule(current, previous /*null when async*/) {// Check if the user is in the specific groupvar userInGroup = gs.getUser().isMemberOf('YOUR_GROUP_NAME');g_scratchpad.showInvestigation = userInGroup;})(current, previous);
Now, we have the data stored in g_scratchpad, and it is ready to be utilized.
Step 2: Use the Scratchpad Variable in an onLoad Client Script
Next, create an onLoad client script to utilize the scratchpad variable and control the visibility of the "Investigation" section.
- Navigate to
System UI > Client Scriptsand click onNew. - Fill out the form with the necessary information:
- Name: Set a descriptive name, such as "Show Investigation Section".
- Table: Select the appropriate table.
- Type: Set to
onLoad.
- In the
Scripttab, use the following script to control the section visibility:function onLoad() {// Check the scratchpad variableif (!g_scratchpad.showInvestigation) {// Hide the Investigation section if the user is not in the groupg_form.setSectionDisplay('section_investigation', false);}}
Replace 'section_investigation' with the actual name or ID of the section you want to control.
Conclusion
By following these steps, you can ensure that the "Investigation" section is only visible to specific group users. This method utilizes a display business rule to set a scratchpad variable and an onLoad client script to control the section's visibility based on the scratchpad data.
Screenshots for reference:
Business Rule:
