# How to get collection variable set in the test scripts to count records in response body?

**URL:** <https://community.postman.com/t/how-to-get-collection-variable-set-in-the-test-scripts-to-count-records-in-response-body/8597>\
**Category:** Help Hub\
**Created:** [October 26, 2019, 4:29am UTC](https://community.postman.com/t/how-to-get-collection-variable-set-in-the-test-scripts-to-count-records-in-response-body/8597 "2019-10-26T04:29:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![balareddiar](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@balareddiar](https://community.postman.com/u/balareddiar)\
**Post date:** [October 26, 2019, 4:29am UTC](https://community.postman.com/t/how-to-get-collection-variable-set-in-the-test-scripts-to-count-records-in-response-body/8597/1 "2019-10-26T04:29:34Z")

</div>

Need help with writing the following test script.  
Verifying the count returned in the response body is equal to 33.

```
var jsonData = JSON.parse(responseBody);
var expectedCount = 33;
tests["Response count is: " + expectedCount] = jsonData.length === expectedCount;

```

The above works, but I want to set `expectedCount` as a collection variable or environmental variable instead of providing the value in the script. How to do that?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [October 26, 2019, 6:42am UTC](https://community.postman.com/t/how-to-get-collection-variable-set-in-the-test-scripts-to-count-records-in-response-body/8597/2 "2019-10-26T06:42:39Z")

</div>

Hey @balareddiar,

Welcome to the community! 🏆

You can set a Collection level variable like this:

`pm.collectionVariables.set("var_name", "var_value")`

You _could_ use that in a test script like this:

```
const jsonData = pm.response.json();

pm.collectionVariables.set("expectedCount", 33)

pm.test("Check the expected length", () => {
    pm.expect(jsonData.length).to.equal(pm.collectionVariables.get("expectedCount"));
});

```

You’ll be able to see what Collection Variables have been set by selecting the `...` icon next to the Collection name in the sidebar, then select `Edit`. This presents you with a popup where you will be able to switch to the `Variables` tab to see the value set.

I’m not sure about your actual use-case so I can only offer a basic solution.

---

<div class="post-metadata">

**Author:** ![balareddiar](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@balareddiar](https://community.postman.com/u/balareddiar)\
**Post date:** [October 26, 2019, 9:12pm UTC](https://community.postman.com/t/how-to-get-collection-variable-set-in-the-test-scripts-to-count-records-in-response-body/8597/3 "2019-10-26T21:12:58Z")

</div>

Thanks, but that did not work for my case. I am trying to count the number of records (say user records) that are shown in the response body after I perform the GET operation. I know the number of users records returned will be certain value, but don’t want to set it in the script, since the value keeps changing and user records number is used in other test scripts.

Like for example, my response body looks like below: 3 records have returned. My web shows 3 users too. So how to verify?  
{  
“id”: 3,  
“username”: “mmm”,  
“user\_number”: 3,  
}  
{  
“id”: 4,  
“username”: “nnnn”,  
“user\_number”: 5,  
}  
{  
“id”: 3,  
“username”: “tttt”,  
“user\_number”: 6,  
}  
I was thinking of setting the number of user records value in Collection variable under Collection: Edit-\>Variable tab and then getting the value in the script. Is that possible?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [October 26, 2019, 9:41pm UTC](https://community.postman.com/t/how-to-get-collection-variable-set-in-the-test-scripts-to-count-records-in-response-body/8597/4 "2019-10-26T21:41:39Z")

</div>

I did mention that I have zero context about what you’re doing 😁 - Your reply has given more information about what you’re trying to do that your original question. 😁

When asking for help or a solution, it’s vital to give the other people, enough detail in order for them to help or you just spend time going back and forth till you get the the actual usecase.

It’s still a little unclear what you trying to do, you mentioned a certain number of records returned and then comparing that to a _web view_ or something like that? What does that look like?

For getting the Collection level variables - You can manually set that and then get the value using `pm.collectionVariables.get("variable_name")` in the sandbox environment.
