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

Inactive_Us1474
Giga Guru

Hi Harry,



var rightBucket = gel(varName + '_select_1'); // varName is name of Slush Bucket.


var selectedOptions = rightBucket.options;



//Get an array of all option IDs


var selectedIDs = new Array();


var index = 0;


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


selectedIDs[index] = selectedOptions[i];


index++;


}


var addMem= new GlideRecord('sys_user_grmember');


for(var j=0; j< selectedIDs.length; j++)


{


addMem.initialize();


addMem.user=selectedIDs[j];


addMem.group=workflow.scratchpad.GroupID;


addMem.insert();


}


//untested do check..



Thanks


Akhil


Hit Like/Helpful/Correct, if applicable.


Deepak Ingale1
Mega Sage

Hi Harry, Since you are working on workflow, you have to use the Server side code to insert the records in sys_user_grmember table. Below code should help you.



var listCollectorIDs = current.variables.bservice_bowners;


var members = listCollectorIDs.split(',');


gs.log("No of users to be added to group " + members.length); // disable this statement for PROD, have it to checks logs i subprod to see waht it prints


var userMembership = new GlideRecord('sys_user_grmember');


for(var i=0 ; i<members.lenght; i++){


            userMembership .initialize();


userMembership.user = members[i];


userMembership.group = workflow.scracthpad.GroupID;


userMembership.insert();


}







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


}


Thank you Abhinay, this worked a charm.



All I was really missing was the .toString()