Form variables display base on Logged in User using onLoad Client script

Peter Williams
Kilo Sage

GOod Day

 

i have a requirement where i have no idea where to go.

 

I have a Request form where different users will use but i want to have certain fields/variable to display or set to read only base on the logged in user Group Membership.

 

ex. John Smith is apart of "Group A"

Jane Doe is apart of "Group B"

 

 

Group A users will only see start date field /variable and the end date field /variable is hidden or read only

While Group B will have the start date field /variable hidden or read only while the end date field /variable they can modify

 

 

how can i:
1) detect the logged in user if they are belonging to a specific group

2) using the IF Statement i can determine if they belong to the Group A or Group B (this is the easy part)

 

i just need to know if they belongs to the group in question or not

1 ACCEPTED SOLUTION

i was able to figure it out with a onLoad client script looking for the role 

 

here is my code:

 

function onLoad() {
    // Get the current user's ID
    var mtlBC = g_user.hasRoleExactly('MTL BC');
    var mtlFS = g_user.hasRoleExactly('MTL FloorSupport');
    if(mtlBC == true){
        g_form.setValue('u_type', 'Business Center');
        g_form.setReadOnly('u_type', true);
    }
    if(mtlFS == true){
        g_form.setValue('u_type', 'Floor Support');
        g_form.setReadOnly('u_type', true);
       
    }

    if(mtlBC == true && mtlFS == true){
        g_form.setValue('u_type', '');
        g_form.setReadOnly('u_type', false);
    }
}
 
this works perfectly

View solution in original post

9 REPLIES 9

Saloni Suthar
Mega Sage
Mega Sage

Hi @Peter Williams,

You can create a Business Rule to find out what group the logged-in user is part of and then use that information from the Business Rule in the client script to hide the variable or make it read-only. 

https://www.servicenow.com/community/itsm-forum/how-to-check-the-logged-in-user-is-a-member-of-a-gro... - You can review this post for script reference. 


If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni

this post doesnt really give that much details on how to do this in the BR nor in the Client script

 

i created this but doesnt seem to work

getting js errors on the console

 

function onLoad() {
  var grpFS = g_user.isMemberOf('77cc2d698790f910124dbbbf8bbb35d1'); //MTL Adjoint(e)s Soutien d'étages
   var grpBC =  g_user.isMemberOf('71ac29298790f910124dbbbf8bbb35cd'); //MTL Centre D'affaires

 
     if (grpFS){
        g_form.setValue('type', 'Floor Support');
        g_form.setReadOnly('type', true);
    }
    if(grpBC){
            g_form.setValue('type', 'Business Center');
            g_form.setReadOnly('type', true);
    }
    if(!grpFS && !grpBC){
        g_form.setReadOnly('type', false);
    }
}

Anand Kumar P
Giga Patron
Giga Patron

Hi @Peter Williams ,

You can create business rule update below script.

var grpusr = current.assignment_group.getDisplayValue();
g_scratchpad.member = gs.getUser().isMemberOf(grpusr);

})(current, previous);

 

Next create a client script on your requirement like On Load or anything.

then in client side

place below script:

if(g_scratchpad.member == true)  {

g_form.setReadOnly('startdate', true);

}

Do same for groupb as well.

Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand