- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2020 02:58 PM
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?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2020 08:35 AM
Hi Brian,
please check below links it should help
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2020 11:08 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2020 07:17 AM
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';
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2020 07:55 AM
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
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2020 08:18 AM
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