How to Dynamically add Role to Group ?

sreejamukherjee
Kilo Expert

Hi All,

I have a requirement to design a catalog item which will automate the process of

1) Creating a new group

2) Add Members to that group

3) Add Role to that group depending on the Type selected from the Item

I am done with the first two requirements but unable to add role to the group dynamically which will be added depending on the Type selected(If the type is "Fulfillment" the role should be "itil" and if the type is "Approval" the role should be "approval_user"). I also want to add the variables that I have used in my item as well as in the "Run Script".

1) Group Name >>(groupname) >>(Single Line Text)

2) Description of the group >> (desc_group) >> (Single Line Text)

3) Select Members >>(select_members) >>(List Collector)

4) Group email >>(group_email) >>(Single Line Text)

5) Support area >>(support_area) >>(Select Box)

5) SA Manager >> (sa_manager) >> (Reference)

6) Vendor Manager >> (vendor_manager) >> (Reference)

7) Type >> (type) >> (Reference)

These all are the variables that I have used in "Run Script" to "Create a new group" and "Add Members to that group".

var gr = new GlideRecord("sys_user_group");

gr.initialize();

gr.name = current.variables.groupname;

gr.description = current.variables.desc_group;

gr.email = current.variables.group_email;

gr.u_support_area = current.variables.support_area;

gr.u_sa_manager = current.variables.sa_manager;

gr.u_vendor_manager = current.variables.vendor_manager;

gr.type = current.variables.type;

var sys_id = gr.insert();

var user = current.variables.select_members;

var user_gr = user.toString().split(",");

for(var i = 0;i<user_gr.length;i++){

var gr1= new GlideRecord("sys_user_grmember");

gr1.initialize();

gr1.user = user_gr[i];

gr1.group = sys_id;

gr1.insert();

}

if(gr.type == 'Fulfillment'){

var gr2 = new GlideRecord("sys_user_role");

gr2.addQuery('name' , 'itil');  

gr2.query();  

if (gr2.next()){

  var gr3 = new GlideRecord('sys_user_group');  

              gr3.addQuery('name' , gr.name);  

              gr3.query();  

              while (gr3.next()){    

  var gr4 = new GlideRecord('sys_group_has_role');  

                      gr4.addQuery('group', gr.sys_id);  

                      gr4.addQuery('role', gr2.sys_id);  

                      gr4.query();  

                      if (gr4.getRowCount() == 0){  

                              var gr5 = new GlideRecord('sys_group_has_role');  

                              gr5.initialize();  

                              gr5.group = gr.sys_id;  

                              gr5.role = gr2.sys_id;  

                              gr5.inherits = true;

  gr4.insert();

                              gr4.insert();

  }

  }

}

}

Please go through with the code and make me correct if I made any mistake. Any help will be highly obliged.

Thanks in advance.

Regards,

Sreeja

1 ACCEPTED SOLUTION

sreejamukherjee
Kilo Expert

Hi Sebastian and Ganesan,


Thanks for your response.


I just added a delay in between Creation of a group and to add Role. Now its working fine.



Regards,


Sreeja


View solution in original post

19 REPLIES 19

You can skip line 1-11;


Test this with a group name which is not already used. Also check wether the group is correctly added (e.g. the field type);



var current = {};


current.variables = {


      groupname: "Test Group 2",


      desc_group: "group",


      group_email: "test",


      support_area: "Area",


      sa_manager: gs.getUserID(),


      vendor_manager: gs.getUserID(),


      type: "Fulfillment",


      select_members: gs.getUserID()


};




var logType = "Role_Script";




try {


      var gr = new GlideRecord("sys_user_group");




      gr.initialize();


      gr.name = current.variables.groupname;


      gr.description = current.variables.desc_group;


      gr.email = current.variables.group_email;


      gr.u_support_area = current.variables.support_area;


      gr.u_sa_manager = current.variables.sa_manager;


      gr.u_vendor_manager = current.variables.vendor_manager;


      gr.type = current.variables.type;


      var sys_id = gr.insert();






      var user = current.variables.select_members;


      var user_gr = user.toString().split(",");


      for (var i = 0; i < user_gr.length; i++) {


              var gr1 = new GlideRecord("sys_user_grmember");


              gr1.initialize();


              gr1.user = user_gr[i];


              gr1.group = sys_id;


              var id = gr1.insert();


              gs.print(id);


      }






      if (gr.type == 'Fulfillment') {


              gs.log("Type is Fulfillment", logType)


              var gr2 = new GlideRecord("sys_user_role");


              gr2.addQuery('name', 'itil');


              gr2.query();


              if (gr2.next()) {


                      gs.log("Found role itil", logType);


                      var gr3 = new GlideRecord('sys_user_group');


                      gr3.addQuery('name', gr.name);


                      gr3.query();


                      while (gr3.next()) {


                              gs.log("Found Group: " + gr3.getValue("sys_id"), logType);


                              var gr4 = new GlideRecord('sys_group_has_role');


                              gr4.addQuery('group', gr3.sys_id);


                              gr4.addQuery('role', gr2.sys_id);


                              gr4.query();


                              if (gr4.getRowCount() == 0) {


                                      gs.log("Role not yet assigned to group", logType);


                                      var gr5 = new GlideRecord('sys_group_has_role');


                                      gr5.initialize();


                                      gr5.group = gr.sys_id;


                                      gr5.role = gr2.sys_id;


                                      gr5.inherits = true;


                                      gr5.insert();


                              }


                      }


              }


      }


} catch (ex) {


      gs.log(ex.message, logType);


}


Hi Sebastian,


I tried with your code but did not work for me and the logs are also not coming from "sys_group_has_role".



Regards,


Sreeja


Can you please provide the logs? Guess you have to debug the code a bit more. Maybe some variable is not set correctly.


Does the group has the correct type?


Hi Sebastaian,


In catalog item the Type field is referenced to "sys_group_type" and have used reference qualifier because I only need Fulfillment and Approval.



Regards,


Sreeja


Ganesan M
Giga Contributor

Hi Sreeja,



Have you tried the suggestion which I provided sometime back in my previous reply,



I would suggest you to have this add role script in after insert business rule of 'Group' table.



find_real_file.png