# Set env variable from script and set next request

**URL:** <https://community.postman.com/t/set-env-variable-from-script-and-set-next-request/22600>\
**Category:** Help Hub\
**Tags:** postman-student-expe\
**Created:** [April 13, 2021, 8:08pm UTC](https://community.postman.com/t/set-env-variable-from-script-and-set-next-request/22600 "2021-04-13T20:08:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![avionics-cosmonaut-2](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@avionics-cosmonaut-2](https://community.postman.com/u/avionics-cosmonaut-2)\
**Post date:** [April 13, 2021, 8:08pm UTC](https://community.postman.com/t/set-env-variable-from-script-and-set-next-request/22600/1 "2021-04-13T20:08:16Z")

</div>

**Your question may already have an answer on the community forum. Please search for related topics, and then read through the [guidelines](https://community.postman.com/t/about-the-help-category/38) before creating a new topic.**

Hi there, Having an issue with the test collection part of student expert. I am getting one failed test case: Set env variable from script and set next request | AssertionError: expected to have a length above 0 but got 0.  
@suesmith please help with it. I have tied all possible things that have been discussed on the community forum regarding this issue…but not able to solve it.  
MY COLLECTION LINK IS : [https://www.getpostman.com/collections/3a681461e5f6328a8f6d](https://www.getpostman.com/collections/3a681461e5f6328a8f6d)

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [April 13, 2021, 10:31pm UTC](https://community.postman.com/t/set-env-variable-from-script-and-set-next-request/22600/2 "2021-04-13T22:31:03Z")

</div>

```
//set var from script and next request - make it env var so we check that too

pm.test("Set env variable from script and set next request", () => {

    //second folder should have set var and next req

    let testsArr = mockArr.filter(value => value.event).map(value => value.event); 

    console.log("testArr",testsArr);

    let execArr = testsArr.flat().filter(value => value.script.exec).map(value => value.script.exec); 

    var setsVars = execArr.flat().filter(p => p.indexOf('environment.set(')>-1); 

    console.log(setsVars);

    pm.expect(setsVars).to.have.length.of.above(0);

    var setsReqs = execArr.flat().filter(p => p.indexOf('.setNextRequest')>-1); 

    console.log(setsReqs);

    pm.expect(setsReqs).to.have.length.of.above(0);

}); 

```

This is the test script that checks that, try to print each lines output and try to understand what it is exactly looking for.

**Hint: it is looking for script.exec in any of the requests and checks whether it has a postman.setNextRequest and pm.environment.**

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [April 13, 2021, 10:34pm UTC](https://community.postman.com/t/set-env-variable-from-script-and-set-next-request/22600/3 "2021-04-13T22:34:16Z")

</div>

> [@Set env variable from script and set next request | AssertionError: expected \[\] to have a length above 0 but got 0](https://community.postman.com/t/set-env-variable-from-script-and-set-next-request-assertionerror-expected-to-have-a-length-above-0-but-got-0/20049):
>
> Heyy all, I am really stuck at this point. After completing all tasks, when I am sending a request for testing my collection, then it shows - Set env variable from script and set next request | AssertionError: expected [] to have a length above 0 but got 0 I’ve updated the collection link, saved each request after every change I made but the same error. Please help me out

---

<div class="post-metadata">

**Author:** ![avionics-cosmonaut-2](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@avionics-cosmonaut-2](https://community.postman.com/u/avionics-cosmonaut-2)\
**Post date:** [April 14, 2021, 7:58am UTC](https://community.postman.com/t/set-env-variable-from-script-and-set-next-request/22600/4 "2021-04-14T07:58:53Z")

</div>

Hi, I have added the code in the second folder “Scripting and Collection Runs”. But there is a failing test case: Set env variable from script and set next request | ReferenceError: mockArr is not defined

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/1/1db7df0c3ce5485123f63d669f4d7214a96fa277.png)

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [April 14, 2021, 8:12am UTC](https://community.postman.com/t/set-env-variable-from-script-and-set-next-request/22600/5 "2021-04-14T08:12:02Z")

</div>

If you have modified the test scripts , revert it back to what it was initially . Don’t modify the scripts in submit request

---

<div class="post-metadata">

**Author:** ![avionics-cosmonaut-2](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@avionics-cosmonaut-2](https://community.postman.com/u/avionics-cosmonaut-2)\
**Post date:** [April 15, 2021, 12:58pm UTC](https://community.postman.com/t/set-env-variable-from-script-and-set-next-request/22600/6 "2021-04-15T12:58:10Z")

</div>

Hi…Thanks for looking into my problem. I have reverted the test script, but the issue of the Test Collection folder still exists.

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [April 15, 2021, 1:09pm UTC](https://community.postman.com/t/set-env-variable-from-script-and-set-next-request/22600/7 "2021-04-15T13:09:34Z")

</div>

You have to click the share collection , and click update link for the changes to get reflected
