How do I create a Client Script that hides Variables on the Catalog Task form from all users except a specific group?

jaredholm
Kilo Contributor

I am looking to create a Client Script that only allows users with the group "ITSS IT Compliance" to view variables on a catalog task form. I currently have this as a test script, but the top half of the if statement is incorrect, hiding the variables from all users except those with the admin role. I need it to allow users with the group "ITSS IT Compliance" to view these variables, but there are no unique roles to filter this group on:

function onLoad() {
      //Get user's group
      if (g_user.hasRole('admin') == true){
     
     } else{ //Set scoring variables to invisible
          g_form.setVisible('score_1', false);
          g_form.setVisible('score_2', false);
          g_form.setVisible('score_3', false);
          g_form.setVisible('score_4', false);
          g_form.setVisible('score_5', false);
          g_form.setVisible('score_6', false);
         g_form.setVisible('score_total', false);
    }
}

The bottom half works. A colleague of mine suggested creating a business rule that gets the user's groups and then calling that business rule, but I am unsure of how this would work, and am hoping for an easier way to do this. If there is no other way, could you explain to me how write the business rule to make this client script work?

Thanks in advance.

1 ACCEPTED SOLUTION

Inactive_Use945
Kilo Expert

Create a display Business rule with script as. Place the below script between function templates in script body.

 

g_scratchpad.grp = gs.getUser().isMemberOf('ITSS IT Compliance');  

 

 

Now update the client script as

 

function onLoad() {

  if (g_scratchpad.grp)){

  g_form.setVisible('score_1', true);
          g_form.setVisible('score_2', true);
          g_form.setVisible('score_3', true);
          g_form.setVisible('score_4', true);
          g_form.setVisible('score_5', true);
          g_form.setVisible('score_6', true);
         g_form.setVisible('score_total', true);

}

    else{ //Set scoring variables to invisible
          g_form.setVisible('score_1', false);
          g_form.setVisible('score_2', false);
          g_form.setVisible('score_3', false);
          g_form.setVisible('score_4', false);
          g_form.setVisible('score_5', false);
          g_form.setVisible('score_6', false);
         g_form.setVisible('score_total', false);
    }
}

 

 

View solution in original post

7 REPLIES 7

Inactive_Use945
Kilo Expert

Hi jaredholm,

 

Can you try this one?

I have used your script, and added some logic: 

Get User ID, Check if user is member of the group and then set the visibility for the required fields.

 

function onLoad() {

  var usr = g_user.getUserID();

  if (usr.isMemberOf('ITSS IT Compliance')){

  g_form.setVisible('score_1', true);
          g_form.setVisible('score_2', true);
          g_form.setVisible('score_3', true);
          g_form.setVisible('score_4', true);
          g_form.setVisible('score_5', true);
          g_form.setVisible('score_6', true);
         g_form.setVisible('score_total', true);

}

    else{ //Set scoring variables to invisible
          g_form.setVisible('score_1', false);
          g_form.setVisible('score_2', false);
          g_form.setVisible('score_3', false);
          g_form.setVisible('score_4', false);
          g_form.setVisible('score_5', false);
          g_form.setVisible('score_6', false);
         g_form.setVisible('score_total', false);
    }
}

 

Hit like if it solves your issue.

This solution does not work. The line "var usr = g_user.getUserID();" is not a valid line of code in a client script and it messes up the client script. With it, all users can see the variables; without it, only admins can see the variables.

Hi,

For this you have to write Display BR on catalog task table with the below code and use g_scratch pad in client script.

 

In Display BR

g_scratchpad.isMember = gs.getUser().isMemberOf('ITSS IT Compliance');    

 

In onLoad  Client script

if(!g_scratchpad.isMember)

{

          g_form.setVisible('variables.score_1', false);
          g_form.setVisible('variables.score_2', false);
          g_form.setVisible('variables.score_3', false);
          g_form.setVisible('variables.score_4', false);
          g_form.setVisible('variables.score_5', false);
          g_form.setVisible('variables.score_6', false);
         g_form.setVisible('variables.score_total', false);



}

 

Inactive_Use945
Kilo Expert

 

The Function " var usr = g_user.getUserID();" works well for me. It returns sys_id

 

You can also try putting it like this

if(gs.getUser().isMemberOf('ITSS IT Compliance'))


Here is the URL for article to show the function utilization