- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2015 11:18 AM
Hello all,
I am trying to collect a list of users in a service form's list collector and to remove said users from the group. I am running this script from within the workflow for the service.
The following is the script snippet I am having issues with.
var users = current.variables.users_remove.toString().split(',');
for(var i=0; i<users.length; i++){
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group', current.variables.group);
//gs.log("user " + i + ": "+ users[i]);
//gr.addQuery('user', users[i]);
gr.query();
//gs.log("gr.next: " + gr.next());
if(gr.next()){ //while there are records in the recordset
var temp = gr.deleteRecord();
// gs.log("TEMP: " + temp);
}
}
The issue I am currently having is that gr.next() is returning false even though I can log the list of users from the group. If anyone could help me out with this I would be quite appreciative.
Thanks,
Jake
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2015 08:43 AM
The result of the list collector is the sys_id of that row of group membership. You will just have to delete that record, I believe.
Again, this was based on my example above, where the variable containing the list of sys_ids of group membership is called "user_test".
function deleteMultipleUsers() {
//This variable will return a list of sys_ids of group membership to delete
var users = current.variables.user_test.toString().split(',');
for(var i=0; i<users.length; i++){
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('sys_id', users[i]);
gr.query();
if(gr.next()){
gr.deleteRecord();
}
}
}
deleteMultipleUsers();
Give that a try?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2015 11:59 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2015 12:20 PM
Jacob, I tested your solution and it worked fine in my instance.
My steps:
1. Create a record producer with a list collector (against user table) and reference field (group table).
2. Create workflow with a run script activity of your updated code.
3. Use record producer to specify 1-3 users and a group that I know they are in.
4. Confirm they were removed.
Are you sure there are users in the groups your specifying to remove? If, for example, you didn't replace users in your test group it would be undefined because you've already removed them.
This wouldn't stop your log statements from correctly retrieving the users from the variables of the record (which is what you're seeing in your 11:59am post), and would explain why the rest are undefined.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2015 08:53 AM
Dylan,
I tried to create a new list collector based on the sys_user table and it worked...my problem, however, is that I have to populate the list collector with the group members currently in the group (to be selected from for deletion from the group); this means that I've had to make the list collector refer to the sys_user_grmember table. Because of this, I am unable to get the query to delete the users.
The issue: I have to populate the list of users currently in the group into the list collector and then I have to delete selected members from said group, however, my script seems to only work for the sys_users table.
If anyone could help me out I would be extremely grateful.
Thanks,
Jake
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2015 08:20 AM
Okay Jacob, I understand what you mean now.
First of all, for the list collector we will use the table "Group Member (sys_user_grmember)". Out of the box, I don't believe it has a display value. In your case, you'll want to set "User" to true (others may chime in here, I've never had to set/change a display value. It seems to work okay).
Once that's done, I consulted the article here: http://www.servicenowguru.com/scripting/client-scripts-scripting/changing-filter-list-collector-vari... to develop the catalog client script below:
NOTE: My list collector table for sys_user_grmember was called "user_test". You'll want to change that below, as well as the name of the variable where the user specifies which group they're after. I called it "group" (I bolded both below).
The script below is on change for whatever the field was where your user can specify a group.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var group = g_form.getValue('group');
//Apply a filter to the list collector variable
var collectorName = 'user_test';
var filterString = 'group='+group;
//Find the filter elements
var fil = gel('ep');
//Hide the filter elements by un-commenting the following lines
/*fil.rows[0].style.display = 'none';
fil.rows[1].style.display = 'none';
fil.nextSibling.rows[0].style.display = 'none'; //Filter description text*/
//Reset the filter query
eval(collectorName + 'g_filter.reset()');
eval(collectorName + 'g_filter.setQuery("' + filterString + '")');
eval(collectorName + 'acRequest(null)');
}
What this will do is when the user specifies a group from the reference field, the filter for the list collector will make it such that only users in that group will be shown, allowing the user to move over whichever users are required.
The rest of the process should be unchanged, except you'll want to make sure you correctly reference the user of the list collector.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2015 08:43 AM
The result of the list collector is the sys_id of that row of group membership. You will just have to delete that record, I believe.
Again, this was based on my example above, where the variable containing the list of sys_ids of group membership is called "user_test".
function deleteMultipleUsers() {
//This variable will return a list of sys_ids of group membership to delete
var users = current.variables.user_test.toString().split(',');
for(var i=0; i<users.length; i++){
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('sys_id', users[i]);
gr.query();
if(gr.next()){
gr.deleteRecord();
}
}
}
deleteMultipleUsers();
Give that a try?