Automate adding users to a Group if the group is assignment group

RichardSaunders
Tera Guru

Hi, i have read lots of posts around automating this process but not quite the same requirements.

We have a Catalogue Request Item called - Add user to group - ServiceDeskOnline

This has two variable sets

Variable Set - SDO_Group

Variable - GroupName

Reference - sys_user_group

Variable Set - essAddUsers

Variable - essAccountUsers

List collector - sys-user

Providing the requested group has Assignment Group checked i would like them added automatically.

group.png

Would someone be able to provide steps to automate this?

Many Thanks

1 ACCEPTED SOLUTION

addUsersToGroup();


function addUsersToGroup()


{


  var myUsers = current.variables.essAccountUsers;


  //gs.log("//\\   list collector: " + myUsers);


  //gs.log("//\\ type of list collector: " + typeof myUsers);


  myUsers = myUsers.toString();


  //myUsers = myUsers.replace(/\s/g, '');


  //gs.log("//\\ type of myUsers: " + typeof myUsers);


  var myUsersInd = myUsers.split(",");  


  //gs.log("//\\ splited part: " + myUsersInd);


  //gs.log("//\\ type of object : " + typeof myUsersInd);


  //gs.log("//\\ first element: " + myUsersInd[0]);


  var countUsers = myUsersInd.length;


  //gs.log("//\\ users count: " + countUsers);


  //gs.log("//\\ Group Name is :" +current.variables.GroupName.getDisplayValue());


  //gs.log(" initial variable list collector: " + myUsers);


  var existingMembers =   [];


  var retrive = new GlideRecord('sys_user_grmember');


  retrive.addQuery('group',current.variables.GroupName);


  retrive.query();


  while(retrive.next())


  {


  existingMembers.push(retrive.user.toString());


  }


  for(var i = 0 ; i <   countUsers ; i++)  


  {  


  var arrUt = new ArrayUtil();


  var grp = new GlideRecord('sys_user_group');


  grp.get(current.variables.GroupName.toString());


  gs.log("//\\ grpName " + grp.sys_id + " " + grp.name + " " + grp.u_assignment_group);


  var member = new GlideRecord('sys_user');


  member.get(myUsersInd[i].toString());


  gs.log("//\\ memberRec " + member.sys_id + " " + member.user_name);


  var grMember = new GlideRecord('sys_user_grmember');


  if(arrUt.indexOf(existingMembers,myUsersInd[i].toString()) == -1)                      


  {


  if(grp.u_assignment_group == true) // if u_assignment_group is checked, then directly add user to group without creating a catalog task      


  {      


  grMember.initialize();          


  grMember.group = current.variables.GroupName;          


  grMember.user = myUsersInd[i];          


  grMember.insert();          


  gs.log("//\\ User " + myUsersInd[i] + " has been added to group   " + current.variables.GroupName);          


  }      


  else   // create a catalog task for manual fulfillment for each user  


  {


  gs.log("//\\ Group is not an assignment group and hence creating task ");


  var ctsk = new GlideRecord('sc_task');      


  ctsk.initialize();      


  ctsk.request_item = current.sys_id;      


  ctsk.assignment_group.setDisplayValue('Database');      


  ctsk.short_description = " Check if user to be added to group or not";  


  ctsk.description = "Please check for user " + member.user_name + " has to be added or not to Group " + current.variables.GroupName.getDisplayValue();


  ctsk.insert();      


  }      


  }      


   


  }


}



Hi Richard,


Above version is working and also made available in your developement instance.


It is working as expected. You can test it throughly.


View solution in original post

18 REPLIES 18

var gr = new GlideRecord('sys_user_grmember');


gr.addQuery('group',current.variables.GroupName);


gr.addQuery('group.u_assignment_group',true); // check if group is assignment group as well


gr.query();


var count = 0;


while(gr.next())


{


      if(current.variables.essAccountUsers.toString()   == gr.user.toString()) // added the missing parenthesis here


    {


                  gs.log("User already exists")


    }


  count++;


}


if(count ==0)


{


    gr.initialize();


    gr.group = current.variables.GroupName;


  gr.user = current.variables.essAccountUsers;


  gr.insert();


  gs.log("User " + current.variables.essAccountUsers + " has been added to group   " + current.variables.GroupName);


}




I have corrected the line 08 based on the screenshot provided by you.


I agree with you, you wont require a catalog task here since it is kind of redundant.



Still, you may create it from process compliance point of view wherein, task will get auto-generated, it will have a work_notes auto populated based on the whether user has been added / already exists etc. but totally depends on how you want to set it up. Not the mandatory thing.


That's seems like a lot of work considering the way we implemented Our instance is heavily based on AD LDAP with a OU dedicated to SNow and user groups in that OU are synced to SNow so membership in a assignment group is automatic once that user is added to the group in AD.


Hi Jeffery,


If you are integrated with AD and managing users , groups and their membership as well via integration only, then you don't have to worry about manual membership management.



Sorry but I could not get your question. Could you please elaborate further on it?


Sorry if I wasn't clear, I had no question and was just offering a different method to achieve Richard's goal.


Okay, got it now.


You wanted to point towards AD integration to accomplish the same.