Add user to group via workflow

Clinton F
Tera Expert

Hi All,

I just want to know if it is possible and how much effort is required to get the following working.

We have allot of requests for ServiceNow access in our company and we would like to automate this process via a Workflow. I want to create a new RITM and a new Workflow to handle these requests. In the workflow we want to be able to ask the user to tell us which group(s) they need to be a member of, this will provide access to them.

This is the part I am not sure I can do or how we would do it. The rest of the workflow would then go for approval first and once approved I want ServiceNow to add them to those group(s) and then close the Request.

Has anyone done something similar and do you know how much effort will be required, hours / days / weeks etc.

15 REPLIES 15

rhea29
Kilo Contributor

Hey i just checked the same and i found out that it is just adding the user to one in the group list and not in all. can you please help


rhea29
Kilo Contributor

I have put on several log on your _pullgroup function and found out that   "Add" has only one group in it. but the grouplist.length is equal to 2


  1. _pullGrps : function(user,grouplist) {  
  2.   var add = [];  
  3.   for (i=0;i<grouplist.length;i++) {  
  4.   var gr = new GlideRecord('sys_user_grmember');  
  5.   gr.addEncodedQuery("user=" + user + "^group=" + grouplist[i]);  
  6.   gr.query();  
  7.   if (!gr.next())  
  8.   add.push(grouplist[i]);  
  9.   return add;  
  10.   }  
  11.   },

Rhea, I don't recommend using the functions from my older post. Use only the ones from my post last week. Alternatively, you can use this code below. Since my last post, I cleaned it up a little further:



In order to prevent group membership being created when one already exists, I added another part to the "addMembership" function.



addMembership: function(user,grouplist) {


    for (var g=0; g < grouplist.length; g++) {


          var check = new GlideRecord('sys_user_grmember');


        check.addQuery('group',grouplist[g]);


        check.addQuery('user',user);


        check.query();



        if (!check.hasNext()) {


            var grp = new GlideRecord('sys_user_grmember');


            grp.newRecord();


            grp.setValue('group',grouplist[g]);


            grp.setValue('user',user);


            grp.insert();


        }


    }


},



askl
Kilo Contributor

I have the same requirement. User can raise a service request(service catalog) and select which group to be added. This has to get approved from change manager and group manager. Only if both approves then the user has to be added in group. Please share with workflow and necessary code.


Shane Butler
Tera Guru

Hi Clinton,

 

I too have been searching for something similar and whilst I am still learning how to do some of this stuff I have found the below link which could look to achieve what you are after:

https://community.servicenow.com/community?id=community_blog&sys_id=3a5da629dbd0dbc01dcaf3231f9619da

 

Again, I have not done this, but I plan to use the above and see if this works for us.

Hope it helps!

 

Kind Regards,

Shane