The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Updating records from arrays in workflows

KB15
Giga Guru

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?

1 ACCEPTED SOLUTION

Dominik Simunek
Tera Guru

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.


View solution in original post

6 REPLIES 6

Venkateswarlu K
Mega Guru

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


Screenshot from 2017-03-02 09_54_20.png




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


Harish KM
Kilo Patron
Kilo Patron

Dominik Simunek
Tera Guru

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.


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?