Set new field based on Roles and Account

juliancoira
Tera Contributor

Hi all,

 

For a specific type of customer we have a few different roles that define what kind of access customer have on our customer support portal. 

 

Some context:

In Account table we have a dropdown called 'Project Type'. Companies with 'Project Type = A' will have normal roles, nothing to be done here. Meanwhile, Company with 'Project Type = B' will have some custom roles that defines different permissions in the Customer Portal and Knowledge Base.

 

I'm trying to generate a report querying the sys_user table and show their corresponding access so Service Managers (or the client) can clearly see who has access to what. The problem is I don't want to show the roles names in the report. My idea was to add a new field that is set using a BR (maybe?) based on the roles, to have more user friendly names displayed. 

 

Rough idea:

if role = A then custom_field = 'User friendly name for Role A'

else if (role =B) then custom_field = 'User friendly name for Role B'

and so on with the other roles.

 

Can that be achieved with a Business Rule? Or someone has a better approach? I'm rather new with ServiceNow so I'm not very familiar with best practices yet.

 

I also want to hide this new field for users from a different project types to avoid having unnecessary info on the form. I think I got that part with a UI Policy tough.

 

Any help with this will be greatly appreciated!  

 

1 ACCEPTED SOLUTION

Aqib4
Kilo Guru

Hi juliancoira,
You can achieve this in ServiceNow using a combination of Business Rules, Client Scripts, and potentially UI Policies. Here’s a structured approach to accomplishing your goal of generating a user-friendly report showing access based on roles without directly displaying role names.

Steps to Implement the Solution

1. Create a New Field in sys_user Table

First, you'll need to create a new field in the sys_user table to store the user-friendly names for roles.

  1. Navigate to: System Definition > Tables
  2. Find and open: sys_user table
  3. Create a new field:
    • Field Name: u_access_description (or something similar)
    • Type: String or Choice (if you have predefined values)

2. Set the Field Value Using a Business Rule

Create a Business Rule that runs on the sys_user_role table to set the u_access_description field based on roles.

  1. Navigate to: System Definition > Business Rules

  2. Create a new Business Rule:

    • Name: Set User Friendly Access Description
    • Table: sys_user_role
    • When to run: After (to ensure the role assignment is saved before processing)
    • Filter Conditions: None (to process all records)
    • Script: Use the following script to set the u_access_description field based on roles:
    (function executeRule(current, previous /*null when async*/) { var user = new GlideRecord('sys_user'); if (user.get(current.user)) { var roleName = current.role.name; // Define user-friendly names based on roles var accessDescription = ''; switch (roleName) { case 'role_A': accessDescription = 'User friendly name for Role A'; break; case 'role_B': accessDescription = 'User friendly name for Role B'; break; // Add cases for other roles as needed default: accessDescription = 'Unknown Access'; } // Update the user record with the friendly name user.u_access_description = accessDescription; user.update(); } })(current, previous);
    • Important: Replace 'role_A' and 'role_B' with actual role names as they appear in your instance.

 

3. Create a Report

  1. Navigate to: Reports > Create New
  2. Select Table: sys_user
  3. Add Columns: Include u_access_description and any other fields you want to report on.
  4. Filter: You may filter users based on Project Type if needed.
  5. Save and Run: Generate the report to display user-friendly access descriptions.

4. Optional: Hide the New Field Using a UI Policy

If you need to hide the u_access_description field from users of different project types on the form:

  1. Navigate to: System UI > UI Policies

    • Table: sys_user
    • Condition: Set a condition based on Project Type (if Project Type is a field on sys_user or another related table).
    • Script (if needed): Use a script to further refine visibility based on roles or conditions.

      Create a new UI Policy:

  2. function onCondition() { var projectType = g_form.getValue('project_type'); // Adjust based on your setup if (projectType != 'Type_B') { // Adjust 'Type_B' as needed g_form.setDisplay('u_access_description', false); } }
     

View solution in original post

2 REPLIES 2

Aqib4
Kilo Guru

Hi juliancoira,
You can achieve this in ServiceNow using a combination of Business Rules, Client Scripts, and potentially UI Policies. Here’s a structured approach to accomplishing your goal of generating a user-friendly report showing access based on roles without directly displaying role names.

Steps to Implement the Solution

1. Create a New Field in sys_user Table

First, you'll need to create a new field in the sys_user table to store the user-friendly names for roles.

  1. Navigate to: System Definition > Tables
  2. Find and open: sys_user table
  3. Create a new field:
    • Field Name: u_access_description (or something similar)
    • Type: String or Choice (if you have predefined values)

2. Set the Field Value Using a Business Rule

Create a Business Rule that runs on the sys_user_role table to set the u_access_description field based on roles.

  1. Navigate to: System Definition > Business Rules

  2. Create a new Business Rule:

    • Name: Set User Friendly Access Description
    • Table: sys_user_role
    • When to run: After (to ensure the role assignment is saved before processing)
    • Filter Conditions: None (to process all records)
    • Script: Use the following script to set the u_access_description field based on roles:
    (function executeRule(current, previous /*null when async*/) { var user = new GlideRecord('sys_user'); if (user.get(current.user)) { var roleName = current.role.name; // Define user-friendly names based on roles var accessDescription = ''; switch (roleName) { case 'role_A': accessDescription = 'User friendly name for Role A'; break; case 'role_B': accessDescription = 'User friendly name for Role B'; break; // Add cases for other roles as needed default: accessDescription = 'Unknown Access'; } // Update the user record with the friendly name user.u_access_description = accessDescription; user.update(); } })(current, previous);
    • Important: Replace 'role_A' and 'role_B' with actual role names as they appear in your instance.

 

3. Create a Report

  1. Navigate to: Reports > Create New
  2. Select Table: sys_user
  3. Add Columns: Include u_access_description and any other fields you want to report on.
  4. Filter: You may filter users based on Project Type if needed.
  5. Save and Run: Generate the report to display user-friendly access descriptions.

4. Optional: Hide the New Field Using a UI Policy

If you need to hide the u_access_description field from users of different project types on the form:

  1. Navigate to: System UI > UI Policies

    • Table: sys_user
    • Condition: Set a condition based on Project Type (if Project Type is a field on sys_user or another related table).
    • Script (if needed): Use a script to further refine visibility based on roles or conditions.

      Create a new UI Policy:

  2. function onCondition() { var projectType = g_form.getValue('project_type'); // Adjust based on your setup if (projectType != 'Type_B') { // Adjust 'Type_B' as needed g_form.setDisplay('u_access_description', false); } }
     

Thank you so much for the quick reply.

This pushed me in the right direction!