How to loop a workflow based on on a list collector for the cmdb_ci_server table

Brian Kimani2
Tera Contributor

I am trying to loop through a number of blocks in a workflow. Basically the user will choose a number of servers from a list collector and the blocks should be executed once per server selected. 

I am using this code in my if block to loop through the list collector:

answer = ifScript();
function ifScript(){
var selectedCIs = current.variables.ci_name.toString();

var array = selectedCIs.split(',');

for (i=0; i<array.length;){
answer = true;
if(answer == true){
workflow.scratchpad.selectedCI = array[i];

return 'yes';
}
return 'no';
}
}

 

The result is an infinite loop that only considers the first item in the list collector. I have attached an image of the section I need to loop.

Any help or ideas on how to solve this?

1 ACCEPTED SOLUTION
7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Brian,

few things to correct

for (i=0; i<array.length; i++){

Also if you want the scratchpad to store all the Cis then you should declare array and store in it

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Brian Kimani2
Tera Contributor

Hi Ankur, I actually corrected that and it led to an infinite loop.

This code is running on an if block and the scratchpad is only storing the first CI on the infinite loop.

answer = ifScript();
function ifScript() {
var selectedCIs = current.variables.ci_name.toString();
//current.work_notes = "slected ci: " + array;
var array = selectedCIs.split(',');
    for (var i = 0; i < array.length; i++) {
   
    if (i<array.length) {
        workflow.scratchpad.selectedCI = array[i];
        return 'yes';
    }
    return 'no';
}
}

Hi Brian,

Is the if condition evaluated again and again?

Are you saying each time you need to get the next ci from the array?

please check below links

https://community.servicenow.com/community?id=community_question&sys_id=c64ce131dbe69380d58ea345ca96...

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Yes Currently I am making the if condition to evaluate again and I think every time it is trying to pick the CIs all over again. Yes I need it to pick the next CI every time it is evaluated. So if I choose 4 CIs from the list collector, the if condition gets evaluated only 4 times. I have no restrictions on what block to use, I just need to find a way to:

1. Store choices chosen from the list collector (CIs)

2. create catalog tasks for each of the CIs

3. Query some attributes of the CIs

4. Ability to store the gathered information