"Export to PDF" in Reports should only be visible to user with admin role

Sachin G K1
Kilo Sage

SachinGK1_1-1684403361658.png

 

Highlighted option in reports should be only visible for user with admin role. How to achieve this?

 

 

 

 

 Thanks in Advance,

Sachin

 

 

4 REPLIES 4

Rahul Mane2
Kilo Guru

Hi Sachin,

 

Try to follow the below step and complete your requirement.

 

To restrict the "Export to PDF" functionality in reports to users with an admin role in ServiceNow, you can customize the report widget by adding a client script. Here's an example of how you can achieve this:

  1. Go to the ServiceNow UI and navigate to the report you want to modify.
  2. Click on the gear icon in the top-right corner of the report and select "Configure" to enter the report configuration mode.
  3. In the report configuration, click on the "Formatters" tab.
  4. Scroll down to the "PDF" formatter section and note the formatter name (e.g., sys_report.PDF).
  5. Close the report configuration.

Next, you can create a client script to control the visibility of the "Export to PDF" option based on the user's role. Here's an example of the client script:

  1. Navigate to "System Definition" > "Client Scripts" in the ServiceNow UI.
  2. Create a new client script record with the following details:
    • Name: Choose a unique name for the client script.
    • Table: Choose sp_report for table.
    • UI Type: Choose All to make the client script available on all UI pages.
    • Type: Choose OnLoad.
    • Script: Use the following script:

 

javascript
(function () { // Check if the current user has the admin role var userRoles = gs.getUser().getRoles(); var isAdmin = userRoles.includes('admin'); // Get the Export to PDF button element var exportBtn = document.querySelector('button[name="sys_report.PDF"]'); // Hide the Export to PDF button if the user is not an admin if (!isAdmin && exportBtn) { exportBtn.style.display = 'none'; } })();

 

Save the client script record. The script will execute on page load and hide the "Export to PDF" button if the current user doesn't have the admin role.

Make sure to test the behavior of the report with a user who has the admin role and with a user who doesn't have the admin role to ensure the "Export to PDF" option is displayed or hidden accordingly.

Please note that the exact steps and script may vary depending on your specific version of ServiceNow. Adjustments may be necessary based on your ServiceNow instance's configuration and requirements.

Hi Rahul,

  1. Click on the gear icon in the top-right corner of the report and select "Configure" to enter the report configuration mode.

I opened the report but there is no gear icon here

 

 

SachinGK1_0-1684406303823.png

 

Rahul Mane2
Kilo Guru

Hi Sachine,

I am trying to figure out if I got any other solution let you know.

 

Rahul Mane2
Kilo Guru

Hi Sachine,

Try below solution

IMG_20230526_155550.jpg