Using list collector variables to create glide records
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2016 09:43 AM
Hello Everyone.
I have request with a list collector where a user selects users to be added to a group.
What is the best way to take the selected users from the list collector and add them to the selected group?
I'm guessing the best way to do this in a workflow would be to create a new glide record on the sys_user_grmember table but I cant figure out how to pass all the sys_id's of the selected users into the script that creates the new record.
Hope that makes sense.
Thanks
Harry
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2016 12:10 PM
That is correct Christopher.
The list collector itself is called bservice_bowners - I assume there wouldn't be a separate variable to 'store' the list of users?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2016 12:54 PM
This may seem a bit too complex, but this solution offers the best way of re-purposing your code as well as keeping your code out of the workflow.
Create a Script Include that you will call from within the workflow. I do this so that you can use these functions in other places as well as in multiple workflows.
Name: groupUtils
Client callable: false
Application: Global
Accessible from: All application scopes
Script:
var groupUtils = Class.create();
groupUtils.prototype = {
initialize: function() {
},
addToGroup: function(usrID, grpID) {
var grmember = new GlideRecord('sys_user_grmember');
grmember.group = grpID;
grmember.user = usrID;
grmember.insert();
},
chkForMembership: function(usrID, grpID) {
return gs.getUser().getUserByID(usrID).isMemberOf(grpID);
},
removeFromGroup: function(usrID, grpID) {
var grmember = new GlideRecord('sys_user_grmember');
grmember.addQuery('group', grpID);
grmember.addQuery('user', usrID);
grmember.query();
while (grmember.next()) {
grmember.deleteRecord();
}
},
type: 'groupUtils'
};
You may want to add your function for creating the group record into this as well. I will leave it up to you to decide.
Since you have the list of users stored from your list collector called bservice_bowners and the group sys_id from your workflow.scratchpad, you can use those in a Run Scripts workflow activity:
var sInc = new GroupUtils(); //initialize the script include
var usrArray = current.variables.bservice_bowners.split(','); // store the list of users as an array so you can work with them easily.
var grpID = workflow.scratchpad.GroupID; // using a shorter variable name
for (u = 0; u < usrArray.length; u++) { // iterate through each slice of the array doing something with each slice
var chkM = sInc.chkForMembership(usrArray[u], grpID);
if (!chkM) { //if user is not a member
sInc.addToGroup(usrArray[u], grpID); //add user to group
}
}
Let me know if you have any questions,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2016 01:35 PM
Thanks for this, it definitely will be useful to have the scrip include.
Unfortunately it's still not working. The group gets created and the workflow progresses without any errors but nobody is added to the group.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2016 01:50 PM
Have you validated that the group you created exists in the Group [sys_user_group] table?
How are you setting the workflow.scratchpad variable. seeing {"GroupID":"f18c8db60f692a002e511efae1050e0a"} makes me wonder how that is being set. See what happens if you add the workflow.scratchpad variable to your logs:
gs.log(workflow.scratchpad.GroupID, 'GroupLog'); //I added 'GroupLog' so that you can quickly find it in the logs.
Also, could you share how your workflow is set up so we understand the order of your flow.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2016 02:25 PM
Yes the group does show on the sys_user_group table.
The group is created with the following script, at the bottom you can see where it is added to the scratchpad:
var newgroup = new GlideRecord('sys_user_group');
newgroup.initialize();
newgroup.name = 'CA_BO_' + current.variables.bservice_name;
newgroup.insert();
workflow.scratchpad.GroupID = newgroup.getUniqueValue();
I added it to the log and it returns the sys_id of the created group:
This is a view of the workflow:
I noticed on the script include, the variable usrID but on the workflow scrip I can see usrID, should these be different?
I'm also thinking it could be a timing issue.....I'm going to add a timer in to wait for 10 seconds before adding the members to the group.