# Challenges with API Runner Loops - Help?

**URL:** <https://community.postman.com/t/challenges-with-api-runner-loops-help/57574>\
**Category:** Help Hub\
**Tags:** variables, collections, json\
**Created:** [February 12, 2024, 8:40pm UTC](https://community.postman.com/t/challenges-with-api-runner-loops-help/57574 "2024-02-12T20:40:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ifritchy](https://avatars.discourse-cdn.com/v4/letter/i/aeb1de/32.png) [@ifritchy](https://community.postman.com/u/ifritchy)\
**Post date:** [February 12, 2024, 8:40pm UTC](https://community.postman.com/t/challenges-with-api-runner-loops-help/57574/1 "2024-02-12T20:40:17Z")

</div>

Hi there! I’m having issues where I need to send multiple identical API calls to my endpoint with different attribute values. I am attempting to do this using multiple runner iterations with a pre-request script. I’ve scrubbed this but this is the behavior I’m getting. My first 4 variables are synchronized though not starting at variable value 0, my 5th variable is totally out of synch with the rest. I am no JS expert by any means, in fact I havent touched JS in about 8 years so I’m struggling quite a bit. I know the .shift() method is going to shift my arrays so I’ve added a prepending null value to each of my arrays however, my 5th variable is still off by X. Any help would be appreciated!!  
{  
“Hotelname”: “{{hotelname}}”,  
“HotelOwner”: “{{hotelOwner}}”,  
“Address”: “{{address}}”,  
“State”: “{{state}}”,  
“zip-code”: “{{zip}}”  
}

let hotelnames = pm.collectionVariables.get(“hotelnames”);  
let hotelOwners = pm.collectionVariables.get(“hotelOwners”);  
let addresses = pm.collectionVariables.get(“addresses”);  
let states = pm.collectionVariables.get(“states”);  
let zips = pm.collectionVariables.get(“zips”);

if(!hotelnames || hotelnames.length == 0){hotelnames = [“Hotel-1”, “Hotel-2” “Hotel-3”, “Hotel-4”, “Hotel-5”];  
}

if(!hotelOwners || 'v.length == 0){hotelOwners = [“HotelBrand-1”, “HotelBrand-2” “HotelBrand-3”, “HotelBrand-4”, “HotelBrand-5”];  
}

if(!addresses || addresses.length == 0){addresses = [“HotelAddress-1”, “HotelAddress-2” “HotelAddress-3”, “HotelAddress-4”, “HotelAddress-5”];  
}

if(!states || states.length == 0){states = [“HotelState-1”, “HotelState-2” “HotelState-3”, “HotelState-4”, “HotelState-5”];  
}

if(!zips || zips.length == 0){zips = [“HotelZip-1”, “HotelZip-2” “HotelZip-3”, “HotelZip-4”, “HotelZip-5”];  
}

let currentHotel = hotelnames.shift();  
pm.collectionVariables.set(“Hotelname”, currentHotel);  
pm.collectionVariables.set(“hotelnames”, hotelnames);

let currentOwner = hotelOwners.shift();  
pm.collectionVariables.set(“HotelOwner”, currentOwner);  
pm.collectionVariables.set(“hotelOwners”, hotelOwners);

let currentAddress = addresses.shift();  
pm.collectionVariables.set(“Address”, currentAddress);  
pm.collectionVariables.set(“addresses”, addresses);

let currentState = states.shift();  
pm.collectionVariables.set(“State”, currentState);  
pm.collectionVariables.set(“states”, states);

let currentZip = zips.shift();  
pm.collectionVariables.set(“zip-code”, currentZip);  
pm.collectionVariables.set(“zips”, zips);

status 200, output:

Hotel-2  
HotelBrand-2  
HotelAddress-2  
HotelState-2  
HotelZip-4

Hotel-3  
HotelBrand-3  
HotelAddress-3  
HotelState-3  
HotelZip-5

Hotel-4  
HotelBrand-4  
HotelAddress-4  
HotelState-4  
HotelZip-1

Hotel-5  
HotelBrand-5  
HotelAddress-5  
HotelState-5  
HotelZip-2

Hotel-1  
HotelBrand-1  
HotelAddress-1  
HotelState-1  
HotelZip-3

* * *

is it possible to force all of my values to an initial value using:

let hotelnames = pm.collectionVariables.get(“hotelnames”);

hotelnames++

let hotelOwners = pm.collectionVariables.get(“hotelOwners”);

hotelOwners++  
let addresses = pm.collectionVariables.get(“addresses”);

adresses++  
let states = pm.collectionVariables.get(“states”);

states++  
let zips = pm.collectionVariables.get(“zips”);

zips++

pm.collectionVariables.set(“Hotelname”,“Hotel-1”)  
pm.collectionVariables.set(“HotelOwner”,“HotelOwner-1”)  
pm.collectionVariables.set(“Address”,“HotelAddress-1”)

pm.collectionVariables.set(“State”,“HotelState-1”)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 13, 2024, 2:24pm UTC](https://community.postman.com/t/challenges-with-api-runner-loops-help/57574/2 "2024-02-13T14:24:44Z")

</div>

Can you edit your post and use the preformatted text option in the editor to post code or JSON.

It stops everything from being aligned to the left, which makes it hard to read, or copy for testing.

Being that the only difference on each run is the number at the end of the variable, I don’t think you need separate arrays for each key. You just need to track the iteration number in your array, and then set the appropriate variables.

You technically don’t need to set any collection variables for this.

For example.

Create a request with a blank body, but set it to raw\JSON.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/3/7/37681b3e8aae515f3f93be7788ff2a0a45ab52e9.png)

The following code is used in the pre-request script.

```auto
if (typeof iterations === 'undefined' || iteration.length == 0) {
    iterations = ["1", "2", "3", "4", "5"];
}

let currentIteration = iterations.shift();

let requestJSON =
{
    "Hotelname": `Hotel-${currentIteration}`,
    "HotelOwner": `HotelBrand-${currentIteration}`,
    "Address": `HotelAddress-${currentIteration}`,
    "State": `HotelState-${currentIteration}`,
    "zip-code": `HotelZip-${currentIteration}`
}

pm.request.body.raw = requestJSON;

if (iterations.length > 0) {
    currentRequest = pm.info.requestName;
    postman.setNextRequest(currentRequest);
} else {
    postman.setNextRequest(null)
}

```

One iteration, run five times.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/b/f/bf7df7612d5c5d949576aa1b2eb70f2125f6b09f.png)

Here is the matching console log, and I’ve expanded out the bodies for request 1 and 5.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/b/5/b5501a667211a06c610d6f7f8d466af218d93fba.png)

---

<div class="post-metadata">

**Author:** ![ifritchy](https://avatars.discourse-cdn.com/v4/letter/i/aeb1de/32.png) [@ifritchy](https://community.postman.com/u/ifritchy)\
**Post date:** [February 13, 2024, 3:15pm UTC](https://community.postman.com/t/challenges-with-api-runner-loops-help/57574/4 "2024-02-13T15:15:23Z")

</div>

So, this was a sanitized example. My script contains complex values for each variable in each iteration, so I can’t simply append an integer for each iteration. I was just trying to show the example that the last variable’s value is out of sync with the rest of the variable values.

Noted for my next post on the preformatted text option though, thanks!

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 13, 2024, 3:49pm UTC](https://community.postman.com/t/challenges-with-api-runner-loops-help/57574/5 "2024-02-13T15:49:01Z")

</div>

You can still use your original approach.

```auto
if (typeof hotelnames === 'undefined' || hotelnames.length == 0) {
    hotelnames = ["Hotel-1", "Hotel-2", "Hotel-3", "Hotel-4", "Hotel-5"]; 
}

if (typeof hotelOwners === 'undefined' || hotelOwners.length == 0) {
    hotelOwners = ["HotelBrand-1", "HotelBrand-2", "HotelBrand-3", "HotelBrand-4", "HotelBrand-5"]; 
}

if (typeof addresses === 'undefined' || addresses.length == 0) {
    addresses = ["HotelAddress-1", "HotelAddress-2", "HotelAddress-3", "HotelAddress-4", "HotelAddress-5"]; 
}

if (typeof states === 'undefined' || states.length == 0) {
    states = ["HotelState-1", "HotelState-2", "HotelState-3", "HotelState-4", "HotelState-5"]; 
}

if (typeof zips === 'undefined' || zips.length == 0) {
    zips = ["HotelZip-1", "HotelZip-2", "HotelZip-3", "HotelZip-4", "HotelZip-5"]; 
}

let currentHotel = hotelnames.shift();
let currentOwner = hotelOwners.shift();
let currentAddress = addresses.shift();
let currentState = states.shift();
let currentZip = zips.shift();

let requestJSON =
{
    "Hotelname": currentHotel,
    "HotelOwner": currentOwner,
    "Address": currentAddress,
    "State": currentState,
    "zip-code": currentZip
}

pm.request.body.raw = requestJSON;

if (hotelnames.length > 0) {  
    currentRequest = pm.info.requestName;
    postman.setNextRequest(currentRequest);
} else {
    postman.setNextRequest(null)
}

```

I’ll just show the console log for the last entry, to show that the values are still in sync.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/0/c098a1b5fc8c335481b48bcb85a0319d6f890526.png)

However, being that all of your arrays have the same number of elements, you only really need to check one of the arrays and you will get the same outcome.

```auto
if (typeof hotelnames === 'undefined' || hotelnames.length == 0) {
    hotelnames = ["Hotel-1", "Hotel-2", "Hotel-3", "Hotel-4", "Hotel-5"]; 
    hotelOwners = ["HotelBrand-1", "HotelBrand-2", "HotelBrand-3", "HotelBrand-4", "HotelBrand-5"]; 
    addresses = ["HotelAddress-1", "HotelAddress-2", "HotelAddress-3", "HotelAddress-4", "HotelAddress-5"]; 
    states = ["HotelState-1", "HotelState-2", "HotelState-3", "HotelState-4", "HotelState-5"]; 
    zips = ["HotelZip-1", "HotelZip-2", "HotelZip-3", "HotelZip-4", "HotelZip-5"]; 
}

let currentHotel = hotelnames.shift();
let currentOwner = hotelOwners.shift();
let currentAddress = addresses.shift();
let currentState = states.shift();
let currentZip = zips.shift();

let requestJSON =
{
    "Hotelname": currentHotel,
    "HotelOwner": currentOwner,
    "Address": currentAddress,
    "State": currentState,
    "zip-code": currentZip
}

pm.request.body.raw = requestJSON;

if (hotelnames.length > 0) {  
    currentRequest = pm.info.requestName;
    postman.setNextRequest(currentRequest);
} else {
    postman.setNextRequest(null)
}

```

---

<div class="post-metadata">

**Author:** ![ifritchy](https://avatars.discourse-cdn.com/v4/letter/i/aeb1de/32.png) [@ifritchy](https://community.postman.com/u/ifritchy)\
**Post date:** [February 13, 2024, 9:24pm UTC](https://community.postman.com/t/challenges-with-api-runner-loops-help/57574/6 "2024-02-13T21:24:41Z")

</div>

Thank you! I used your example with the multiple arrays using typeof and that did the trick! I forgot to set my Test and thus I didn’t have my:  
`pm.test("Status code is 201", function () { pm.response.to.have.status(201); }`  
So it looped beyond where I wanted it to go. But, it stayed in sync finally!! Really appreciate the assist here!

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 14, 2024, 9:56am UTC](https://community.postman.com/t/challenges-with-api-runner-loops-help/57574/7 "2024-02-14T09:56:21Z")

</div>

The reason we use (typeof hotelnames === ‘undefined’) instead of (if !hotelnames) is that the latter will cause a fatal error if the variable doesn’t exist and stop any other code from running, where typeof will check that the variable exists, that its not null, or undefined and not cause a fatal error when it runs, so any other code will still run.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [February 17, 2024, 9:56am UTC](https://community.postman.com/t/challenges-with-api-runner-loops-help/57574/8 "2024-02-17T09:56:38Z")

</div>

Closed on February 17, 2024, 9:56am UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
