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

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @Michael Galang !

 

Try to add all the conditions in a single if condition and then try to show / hide realted list.

 

Test by adding logs, > console.log('passed')

Add one condition at a time and see which condition is not evaluating to true. 

 

Example

 

If(g_user.hasRole('1bd05f2e1bd2630083fe2fcdee4bcb47') || g_user.hasRole('df8dc7dedb3833005777255c8a9619ac') || g_form.getValue('state') == '5' || g_form.getValue('u_case_type') == 'investigation' || <add more> ) {

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

}


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

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

Hi Maddysunil, 

 

Thanks you. It works now. 

Amit Gujarathi
Giga Sage
Giga Sage

HI @Michael Galang ,
I trust you are doing great.
To control the show/hide behavior of the related list ('task_sla.task') based on user roles and other conditions, we can make the following adjustments to your client script:

function onLoad() {
    var hasRequiredRole = g_user.hasRole('role1_name') || g_user.hasRole('role2_name'); // Replace 'role1_name' and 'role2_name' with actual role names
    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 (optional)
    console.log('hasRequiredRole: ' + hasRequiredRole);
    console.log('caseState: ' + caseState);
    console.log('caseType: ' + caseType);
    console.log('hrService: ' + hrService);

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

    console.log('additionalConditionsMet: ' + additionalConditionsMet);

    // Check if all conditions are met to show/hide the related list
    if (hasRequiredRole && additionalConditionsMet) {
        g_form.showRelatedList('task_sla.task');
    } else {
        g_form.hideRelatedList('task_sla.task');
    }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi