REST call in a FOR loop

Kristiyan Niko2
Kilo Explorer

Hi all, 

Has anyone seen this issue that if you put a scripted REST message in a FOR loop, the loop does only one iteration and never proceeds to do the rest of the iterations, even though the loop has to do several iterations more? 

I've seen several threads like this in the community, but nobody seems to answer.

Just wondering if anyone has any idea. 

It's not a syntax or logic issue. The loop does all iterations if I remove the REST message from it. If it is in the loop, it does only the first iteration and the workflow finishes without any errors. 

Thanks

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Nikolov,

As in for loop the rest message script should evaluate as many number of times as defined in for loop.

Can you share the script here?

I think ServiceNow might be restricting the REST message call in for loop because it may take longer time to execute since you are calling some API and are waiting for the API response.

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Hi Ankur, 

 

The issue seems like a nasty bug. The thing which I did that solved the issue was the following:

 

Replaced

for (var i = 0; i < servers.length; i++) with for (var a = 0; a < servers.length; a++)

 

The variable "i" was never used anywhere else in the script so that's why i think it's a bug.

 

After replacing i witha a the loop iterated as many times as there were items in the array. Just for testing purposes I switched the a back to i and the issue presented itself again.

 

Hope this helps someone else. 

otog
Tera Contributor

Hi

 

i is an implicit variable, use some other variable.

 

Thanks,

Srujit Biradawada.