Managing ER Cases by ER Agents - ViewRule

In many organizations, HR Employee Relations (ER) cases are created and fulfilled exclusively by the HR team. Out-of-the-box (OOB), when an agent creates a case in ServiceNow and navigates to the ER Case, the default view shown is the self-service view. This view renders everything as read-only, which prevents the agent from effectively working on the case.

To address this issue, I implemented a solution by adjusting the view rules to allow agents to access and work on the cases appropriately. Below, I share the approach and the code used to achieve this.

Default View for Grievance Cases

The following script overrides the default view for grievance cases, allowing agents with the sn_hr_er.case_writer role to access the appropriate view:

(function overrideView(view, is_list) {
answer = view;

var hrservice = '';
var id = gs.action.getGlideURI().get('sys_id');
if (answer != 'sp' && answer != 'esc' && !is_list && id) {
var gr = new GlideRecord('sn_hr_er_case');
if (gr.get(id)) {
hrservice = gr.getValue('hr_service'); // 0b0234234a261a4bcb8e - Grievance
}
}

if (gs.hasRole('sn_hr_er.case_writer') && (hrservice == '2340b0234234a261a4bcb8e' || hrservice == '3451907c3c524d0d4bcb19')) {
answer = "default";
}
})(view, is_list);


Ensuring Non-ER Employees See Only the ESS View

To ensure that non-ER employees only see the Employee Self-Service (ESS) view, the following script is used:

(function overrideView(view, is_list) {
answer = view;
if (!gs.hasRole('sn_hr_er.case_reader') && answer != 'sp')
answer = "ess";
})(view, is_list);

Explanation of the Scripts

  1. Default View for Grievance Cases:

    • This script checks if the view is not sp (self-service) or esc (employee center), the case is not in a list, and a valid sys_id is present.
    • It then retrieves the HR service value for the case.
    • If the agent has the sn_hr_er.case_writer role and the HR service matches specific grievance case IDs, the view is set to "default".
  2. Non-ER Employees Only See ESS View:

    • This script checks if the user does not have the sn_hr_er.case_reader role and if the view is not sp.
    • If both conditions are met, the view is set to "ess" (Employee Self-Service).

By implementing these scripts, we ensure that agents can efficiently work on HR ER cases while maintaining appropriate access controls for non-ER employees.

Feel free to reach out if you have any questions or need further clarification.