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

Deepak Ingale1
Mega Sage

Hi Richard,



You can try below code



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


    {


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


}


Hi Deepak,



Sorry, excuse my ignorance. coding not my strong point!



Is that an On Submit (sys_user_grmember table- Client Script?


Also i need for the Catalogue Task state to Close Complete.


No Problem Richard,



The things we are trying to automate are Server Side. This can be best achieved via



1) Business rules :- If you do not have a workflow associated with the item you are submitting



2) Workflow scripting.:- You can use the "Run Script" activity of workflow.


You can close the catalog task as well in the same script.



Code would be



var tsk = new GlideRecord('catalog_task');


tsk.addQuery('request_item',current.sys_id); // I expect only single catalog task is there for this process which needs to be auto closed


tsk.query();


while(tsk.next())


{  



  tsk.state = "integer value for closed complete' // e.g. tsk.state = 4 ; etc


tsk.update();


}


Thanks for the response.



I think it might be best to create a new workflow for this item as i dont think it will need a task after-all.



So i think a pretty basic workflow with the code (above) to add the user will do it. I tried adding the code to add them to the group but i get this error back.



Untitled.png