- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2016 03:53 AM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2016 08:24 AM
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();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2016 05:40 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2016 08:00 AM
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();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2016 08:24 AM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2016 10:03 AM
Thank you Abhinay, this worked a charm.
All I was really missing was the .toString()