Custom tab on different CI tables should be visible for users who have custom role

kurella swamy
Tera Contributor

Hi 
I have created 'Asset Lifecycle'(asset_lifecycle) section on cmdb_ci_computer',cmdb_ci_server','cmdb_ci_win_server','cmdb_ci_ip_firewall', 'cmdb_ci_ip_switch','cmdb_ci_ip_device','cmdb_ci_wap_network and added custom fields which are created on cmdb_ci table. so users who has these custom role"x_cmdb_write" should only see the section
For that I have written below script incude and 7 client scripts on each table by calling same script include Now it is working fine.
Script inlcude:-

var RoleCheckAssetlifecycle = Class.create();
RoleCheckAssetlifecycle.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    accessCheck: function() {


        var usersysID = this.getParameter('usersysID');
        var roles = 'x_cmdb_write';


        var userRole = new GlideRecord("sys_user_has_role");
        userRole.addEncodedQuery('user.sys_id=' + usersysID + '^role.nameIN' + roles);
        userRole.query();
        if (userRole.next()) {
            return true;
        } else {
            return false;
        }

    },

    type: 'RoleCheckAssetlifecycle'
});
Client script:- 
function onLoad() {

    var gr = new GlideAjax('RoleCheckAssetlifecycle');
    gr.addParam('sysparm_name', 'accessCheck');
    gr.addParam('usersysID', g_user.userID);
    gr.getXMLAnswer(getResponse);

    function getResponse(response) {
        var answer = response;
        if (answer == 'true') {

            g_form.setSectionDisplay('asset_lifecycle', true);


        } else {
            g_form.setSectionDisplay('asset_lifecycle', false);
        }
    }
}

But the requirement is please consolidate the 7 client scripts you created on each table to a single client script on the cmdb_ci table . How we can do this one
Tried with below script on cmdb_ci table but not wrking

function onLoad() {
    var allowedTables = [
        'cmdb_ci_computer',
        'cmdb_ci_server',
        'cmdb_ci_win_server',
        'cmdb_ci_ip_firewall',
        'cmdb_ci_ip_switch',
        'cmdb_ci_ip_device',
        'cmdb_ci_wap_network'
    ];
  var table = g_form.getTableName();
    if (!allowedTables.includes(table)) {
        console.log("Not a target table. Exiting.");
        return;
    }
var ga = new GlideAjax('RoleCheckAssetlifecycle');
    ga.addParam('sysparm_name', 'accessCheck');
    ga.addParam('usersysID', g_user.userID);

    ga.getXMLAnswer(function(response) {
        var visible = response === 'true';
 try {
            g_form.setSectionDisplay('asset_lifecycle', visible);
         
        }
         
    });
}

Any suggestions would be greatly appreciated. Thanks in advance 





1 ACCEPTED SOLUTION

@kurella swamy 

in normal client script did you check "Inherited" so that it runs on extended tables

Also did you add logs?

Also ensure the table on which you are running that form section doesn't have any mandatory fields? if it has then it won't hide the form section

AnkurBawiskar_0-1746623771747.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

5 REPLIES 5

@kurella swamy 

Glad to help.

Would you mind closing your earlier question by marking appropriate response as correct?

How we can hide the CI asset tab on the for users who dont have a customer role 

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