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

Deepak Kumar5
Kilo Sage

Create After Business Rule on sys_user_grmember table for insert/update/delete.



Check the User hasRole("ITIL") then make the checkbox true/false based on result.


For more specific, Check user have ITIL role or not.if yes, then check that user record for that field and update if required.


Dave Smith1
ServiceNow Employee
ServiceNow Employee

I'd question the reasoning behind the purpose of this checkbox - it sounds like you're duplicating effort (and data).



If the checkbox needs to be true if the user holds the itil role, why not just perform the check against that role?


dvp
Mega Sage
Mega Sage

You have to write after business rule when the record is inserted and deleted on sys_user_has_role table



find_real_file.png



Here is the code. Please try it and let us know if you have any roadblocks.



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



      // Add your code here



      var gr = new GlideRecord("sys_user_has_role");


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


      gr.query();


      var str = [];


      while(gr.next()) {


              str.push(gr.role.name.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', current.user);


          grd.query();



          while(grd.next()){


              grd.YOUR_FIELD_NAME = has_itil;


              grd.update();


          }




})(current, previous);


Hi Dvp,


                      Thanks for your Idea, But this is not working , this is working only when the role is added trough user table(sys_user), I want when role is added trough groups(sys_user_group).


Any suggestions.



Thanks.