Scratchpad to Control Section Visibility in ServiceNow

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.

  1. Navigate to System Definition > Business Rules and click on New.
  2. 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.
  3. Add a condition to ensure this business rule runs only when needed.
  4. In the Script tab, use the following script to set a scratchpad variable:

    (function executeRule(current, previous /*null when async*/) {
    // Check if the user is in the specific group
    var 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.

  1. Navigate to System UI > Client Scripts and click on New.
  2. 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.
  3. In the Script tab, use the following script to control the section visibility:

    function onLoad() {

    // Check the scratchpad variable
    if (!g_scratchpad.showInvestigation) {
    // Hide the Investigation section if the user is not in the group
    g_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:










Client Script: