Control show/hide of related list using Client script

Michael Galang
Tera Contributor

Hi Experts, 

 

I have created a below client script to show/hide the related list ('task_sla.task') based on the g_user roles and other conditions specified below.  The target table is sn_hr_core_case_relation

However, when I impersonate a user that doesn't have the required roles, the related list is still showing. 

I would appreciate any help to fix it. Thanks in advance. 

function onLoad() {
var hasRequiredRole = g_user.hasRole('1bd05f2e1bd2630083fe2fcdee4bcb47') || g_user.hasRole('df8dc7dedb3833005777255c8a9619ac');
var caseState = g_form.getValue('state');
var caseType = g_form.getValue('u_case_type');
var hrService = g_form.getValue('hr_service');

// Add your additional conditions here
var additionalConditionsMet = caseType == 'investigation' && hrService == 'Intake' && (caseState == '5' || caseState == '32' || caseState == '33');

if (additionalConditionsMet) {
if (hasRequiredRole) {
g_form.showRelatedList('task_sla.task');
} else {
g_form.hideRelatedList('task_sla.task');
}
}
}

1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@Michael Galang 

I have made few corrections and applied few alerts message to inspect the value you'r fetching , Below is the code:

 

function onLoad() {
    var hasRequiredRole = g_user.hasRole('role1_name') || g_user.hasRole('role2_name');//provide the role name instead of sysid
    var caseState = g_form.getValue('state');
    var caseType = g_form.getValue('u_case_type');
    var hrService = g_form.getValue('hr_service');

    // Debug statements to check values
    alert('hasRequiredRole: ' + hasRequiredRole);
   alert('caseState: ' + caseState);
    alert('caseType: ' + caseType);
   alert('hrService: ' + hrService);

    // Add your additional conditions here
    var additionalConditionsMet = caseType == 'investigation' && hrService == 'Intake' && (caseState == '5' || caseState == '32' || caseState == '33');
    gs.info('additionalConditionsMet: ' + additionalConditionsMet);

    if (additionalConditionsMet) {
        if (hasRequiredRole) {
            g_form.showRelatedList('task_sla.task');
        } else {
            g_form.hideRelatedList('task_sla.task');
        }
    }
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

View solution in original post

7 REPLIES 7

Hi Amit, 
Thanks you. It works now. 

Ankur Bawiskar
Tera Patron
Tera Patron

@Michael Galang 

what debugging have you done so far?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

thoang93
Tera Expert

Hi @Michael Galang,

Your script looks good for function, it works in my instance. It would help if you tracked input value by log to ensure it is correct. On the other hand, the impersonated user should check 2 condition roles added. it will WORK.

 

Please Mark ✔️Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks & Best Regards,

Peter Tran