Help in Client Script: If user belongs to CMDB assignment group, he can edit a field in cmdb table

ArushiG
Tera Contributor

Hi team,

Please help me with a client script on cmdb_ci_hardware table:

 

If a user belongs to cmdb_ci_hardware table, he should be able to edit the new field "External IP". I've written the below:

 

function onLoad() {
   //Type appropriate comment here, and begin script below


    var usrdetail = g_user.getUserID();
    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('user', usrdetail);
    gr.addQuery('group', '47d86fe1db7f845055434cb11596192a');
    gr.query();
   
    if(gr.next()){
        g_form.setVisible('u_external_ip',true);
        g_form.setReadOnly('u_external_ip', false);
    }
       
    else{
        g_form.setVisible('u_external_ip',false);
        g_form.setReadOnly('u_external_ip', true);
    }
   
}
13 REPLIES 13

@ArushiG 

I have shared 2 approaches, 1st is ACL based (recommended) and 2nd is using onLoad client script + Display business rule

Please check the same below

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

Ankur Bawiskar
Tera Patron
Tera Patron

@ArushiG 

2 Approaches

1) Field level WRITE ACL, use advanced script

answer = gs.getUser().isMemberOf('Group ABC');

2)  you can use onLoad client script + Display business rule on "cmdb_ci_hardware" to check if user belongs to particular group or not

Display business rule

(function executeRule(current, previous /*null when async*/ ) {
    // Pass the result to g_scratchpad
    g_scratchpad.canEditExternalIP = gs.getUser().isMemberOf('Group ABC');
})(current, previous);

onLoad client script

function onLoad() {
    // Check if g_scratchpad indicates permission to edit External IP
    if (g_scratchpad.canEditExternalIP) {
        g_form.setReadOnly('external_ip', false); // Enable editing
    } else {
        g_form.setReadOnly('external_ip', true); // Disable editing
    }
}

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

Thanks a lot Ankur, I'll go online in few minutes and will update after trying

@ArushiG 

Sure do keep me updated !

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