- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-01-2017 07:51 PM
I've tried this script with a list of sys_ids to test in a background script and works perfectly, but when I try to run this in a script activity or advanced script portion in a task, it doesn't work. In a task, it's telling me that splits aren't available. As a script, it'll just run it but does nothing.
I'm getting my list from a list collector variable.
var list = current.variables.u_hold_hardware;
var array = list.split(",");
for (var i=0; i < array.length; i++) {
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', array[i]);
gr.query();
while (gr.next()) {
gr.u_hold_hardware = true;
gr.update();
}
Also, is there a best practice around modifying multiple records other than through a GlideRecord query?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2017 01:33 AM
Hi kkim,
Can you please log in the workflow what value is available in current.variables.u_hold_hardware to be sure that there is a value?
I would also try to add "toString()" to be sure that split method is available. The code itself can be optimized however, because if your "u_hold_hardware" variable contains sys_ids of users, you do not need to split it at all. The query can look like this:
var userGR = new GlideRecord('sys_user');
userGR.addQuery('sys_id', 'IN', current.variables.u_hold_hardware.toString()); // expecting that variable contains comma separated list of sys_ids
userGR.query();
while (userGR.next()) {
userGR.u_hold_hardware = true;
userGR.update();
}
This query should return all users defined by sys_id in the variable. The benefit is better performance as instead of multiple database select queries, you have just one.
Let me know if it works for you - I did not test the code on my instance, but I hope there is no typo or other error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-01-2017 08:27 PM
Hi kkim,
Can u please share some snapshots or elaborate what ur exact requirement i worked similar to this type of a req.that contains a two variables 1.group(ref.type) and 2.users(list clooector type ) what ever the selected users from the list collector will addung to the selected group from the ref variable for that purpose am using a run script in work flow script look like this
var users= current.variables.user.toString().split(','); //list of users selected from the list collector
for(i=0; i<users.length; i++){
var gr= new GlideRecord('sys_user_grmember');
gr.initialize();
gr.group=current.variables.group; //adding users to the corresponding group
gr.user=users[i];
gr.insert();
}
Please mark as a correct or mark as a helpful if it is useful
Thanks&Regards,
Venkateswarlu Kuruva

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-01-2017 08:48 PM
Re: Loop thought list collector
How to populate the name of a sys_id which has been split
these threads should be useful.
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2017 01:33 AM
Hi kkim,
Can you please log in the workflow what value is available in current.variables.u_hold_hardware to be sure that there is a value?
I would also try to add "toString()" to be sure that split method is available. The code itself can be optimized however, because if your "u_hold_hardware" variable contains sys_ids of users, you do not need to split it at all. The query can look like this:
var userGR = new GlideRecord('sys_user');
userGR.addQuery('sys_id', 'IN', current.variables.u_hold_hardware.toString()); // expecting that variable contains comma separated list of sys_ids
userGR.query();
while (userGR.next()) {
userGR.u_hold_hardware = true;
userGR.update();
}
This query should return all users defined by sys_id in the variable. The benefit is better performance as instead of multiple database select queries, you have just one.
Let me know if it works for you - I did not test the code on my instance, but I hope there is no typo or other error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2017 09:50 AM
Thanks for the tip. This works and less complicated than the array route.
So if the list is already in a comma separated format, you don't need split out each entry? In what situations would you want to split? Also, if the list collector isn't in a string format, what format is it in?