Hi Team, I have requirement where there is a check box in User table , when ever user is given with ITIL role this check box should be automatically checked.

srikanth241
Mega Expert

Hi Team,

I have requirement where there is a check box in User table , when ever user is given with ITIL role this check box should be automatically checked. If Itil role is removed automatically this check box should be unchecked.

Roles are added to groups.

some times user may have two groups, Both the groups may have ITIL role,In case in any one of the group is removed from ITIL role then Check box should not be unchecked as he has role from other group.

Any Idea would greatly help me.

Thanks,

AB.Community CornerExperts CornerPlatformDiscuss

1 ACCEPTED SOLUTION

Ok... if you want to to see the same update on user record when the role is directly added to group, you have to write a same business rule as above with little modification, on sys_group_has_role table.


find_real_file.png


Here is the code.. test it and let us know



(function executeRule(current, previous /*null when async*/) {



      // Add your code here



      var grp = new GlideRecord("sys_user_grmember");


      grp.addQuery('group', current.group);


      grp.query();


   


      while(grp.next()){


      var gr = new GlideRecord("sys_user_has_role");


        gr.addQuery('user', grp.user);


      //gr.addQuery('role.name', 'itil');


      gr.query();


var str = [];


      while(gr.next()) {



              str.push(gr.role.name.toString());



      }



      gs.log('TEST ' + str.toString());


   


      var arrayUtil = new ArrayUtil();


      var pos = arrayUtil.indexOf(str, 'itil');


   


      var has_itil = '';


   


      if( pos < 0){


              has_itil = 'false';


      }


      else


              has_itil = 'true';


   


      var grd = new GlideRecord('sys_user');


            grd.addQuery('sys_id', grp.user);


            grd.query();



            while(grd.next()){


              grd.YOUR_FIELD_NAME = has_itil;


              grd.update();


            }



      }


})(current, previous);


also the initial given code will not work when is user is added/removed from the group. All you need to do to make it work is, write a business rule on sys_user_grmember table with the same coditions and script that we wrote initially on sys_user_has_role table.




After you have made all the above changes, let us know your final results



THanks!!


View solution in original post

8 REPLIES 8

Ok... if you want to to see the same update on user record when the role is directly added to group, you have to write a same business rule as above with little modification, on sys_group_has_role table.


find_real_file.png


Here is the code.. test it and let us know



(function executeRule(current, previous /*null when async*/) {



      // Add your code here



      var grp = new GlideRecord("sys_user_grmember");


      grp.addQuery('group', current.group);


      grp.query();


   


      while(grp.next()){


      var gr = new GlideRecord("sys_user_has_role");


        gr.addQuery('user', grp.user);


      //gr.addQuery('role.name', 'itil');


      gr.query();


var str = [];


      while(gr.next()) {



              str.push(gr.role.name.toString());



      }



      gs.log('TEST ' + str.toString());


   


      var arrayUtil = new ArrayUtil();


      var pos = arrayUtil.indexOf(str, 'itil');


   


      var has_itil = '';


   


      if( pos < 0){


              has_itil = 'false';


      }


      else


              has_itil = 'true';


   


      var grd = new GlideRecord('sys_user');


            grd.addQuery('sys_id', grp.user);


            grd.query();



            while(grd.next()){


              grd.YOUR_FIELD_NAME = has_itil;


              grd.update();


            }



      }


})(current, previous);


also the initial given code will not work when is user is added/removed from the group. All you need to do to make it work is, write a business rule on sys_user_grmember table with the same coditions and script that we wrote initially on sys_user_has_role table.




After you have made all the above changes, let us know your final results



THanks!!


Thanks Dvp, Its working


hussain h1
Tera Contributor

Hello Dvp,

 

The above given code which has written on "sys_user_grmember" is only working when we are providing the ITIL role to user, but its not unchecking the box when we are removing the related group from user.

 

Kindly suggest.

In order to do it, you should make this Business Rule to be fired "on delete" and also, add the logic to check if the user is still having "itil" role (given by any other group, or just directly) and confirm, so, in few words, the script should be like this:

var gr = new GlideRecord('sys_user_has_role');
gr.addQuery("role","itil")
gr.addQuery("user", current.user.sys_id.toString());
gr.query()


var grUser = new GlideRecord('sys_user');
if ( gr.get( current.user.sys_id.toString() ) ) {
   grUser.some_variable = gr.getRowCount() >= 1?true:false;
   grUser.update();
}

This script shouldn't work if the user is changed "manually" directly in the [sys_user_has_role] register, but there is no sense to change neither the user nor the role directly in those registers, being the intended unique operation to do there to insert or to delete the entire record.

 

Hope it helps