Creating a group and adding users using list collector

Harry Campbell2
Mega Guru

I did ask this yesterday but after going back and forth with some very helpful people I still haven't managed to get this working. Here's the scenario:

I have a cat item with the following variables:

bservice_name - string

bservice_bowners - list_collector (list of users)

Once submitted - the cat item is sent for approval, once approved, the workflow creates a new group using the bservice_name, this is the code:

var newgroup = new GlideRecord('sys_user_group');  

newgroup.initialize();

newgroup.name = 'CA_BO_' + current.variables.bservice_name;    

newgroup.insert();

workflow.scratchpad.GroupID = newgroup.getUniqueValue();

The new group is created without any issues and the sys_id of the new group is written to the scratchpad.

The next part is where I'm getting stuck, I need the users selected in the list collector (bservice_bowners) to be added to the new group.

The return from the list collector is stored as comma seperated sys_id's. I need to create a new record on the sys_user_grmember table for each of the users selected on the list collector.

The only way I believe I can do this is using an array.....but all the suggestions in the previous thread dont work. No record is created on the sys_user_grmember table.

Any help would be appreciated.

Many Thanks

Harry

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

Here is your code



var users= current.variables.bservice_bowners.toString().split(',');


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


var gr= new GlideRecord('sys_user_grmember');


gr.initialize();


gr.group=workflow.scratchpad.GroupID;


gr.user=users[i];


gr.insert();


}


View solution in original post

6 REPLIES 6

Glad you got it working.


Hey Abhinay Erra thanks a lot for this solution. 

I had a similar problem and your comment helped me to solve it.

I alredy saved it in my presonal scripts library, thank you very much.