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

List collector stores the selected values as comma-separated string of sys_ids. Then you can do whatever you want with such string. But as "addQuery" with "IN" operator works with comma-separated string, you can use it directly in the query.


The split could be used if you need to somehow work with sys_ids one-by-one (I don't have any scenario in my mind now). But here you want to work with GlideRecords directly so you can retrieve all of them in one query.


That was a very helpful response. Thank you!