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

Thanks Jeffrey, we have over 80k users and the directory is structured in such a way that they inherit group polices from being in a certain OU. Creating a new OU for SN groups( and getting the group polices changed over) would be a bit of a nightmare. Especially having to raise a change that may get rejected. Especially with our meticulous change team.


RichardSaunders
Tera Guru

Thanks Depak, i added the amended code and it completed without an error. However the user i asked to be added to the group did to add.



I have just had a thought.... It will need a task creating if u_assignment_group is false for a team to manage manually. And agreed from a complaince POV a task created with work notes confirming that it was updated.



i created the task, how would i write the condition?



Thanks in advance.



find_real_file.png


Hi Richard,



Did the above code work to add user to group?



With your new requirement, you can write down the below code.


Note:- You wont require to add workflow activity 'catalog task' for creation of catalog task. Script itself will take care to do it.


If u_assignment_group is true, user will be added automatically, else catalog task will get generated for manual check.



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 is a group")  


    }  


  count++;  


}  


if(count ==0)  


{  


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


  {


    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);  


  }


  else


  {


  var ctsk = new GlideRecord('sc_task');


  ctsk.initialize();


  ctsk.request_item = current.sys_id;


  ctsk.assignment_group.setDisplayValue('Assignment Group Name should go here');


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


              ctsk.insert();


  }


}  


Hi Deepak,



I have changed the code from your last post. However the workflow shows no errors but no users are added to the group. I tried this with a number of different groups (i also removed the failing notification workflow activity incase that had any impact.



Thanks again for your help with this. any ideas?


Hi Richards,



are you trying this out in Personal developer instance or demo instance of servicenow?


If yes, then could you please give me an access to check what is happening?



If not, then at this moment, i can tell you to check if variables name reference in the script are same as those configured in the system.


Did you get any log generated in system logs which i tried to insert via script?