# Using collection variable in test

**URL:** <https://community.postman.com/t/using-collection-variable-in-test/22538>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [April 12, 2021, 7:58pm UTC](https://community.postman.com/t/using-collection-variable-in-test/22538 "2021-04-12T19:58:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![timothy.jahn](https://avatars.discourse-cdn.com/v4/letter/t/ccd318/32.png) [@timothy.jahn](https://community.postman.com/u/timothy.jahn)\
**Post date:** [April 12, 2021, 7:58pm UTC](https://community.postman.com/t/using-collection-variable-in-test/22538/1 "2021-04-12T19:58:05Z")

</div>

Hi,

I am having problems trying to use collection variable as an assertion in a test. I think my syntax is off.

Collection variables:

 ![collectionVars](https://us1.discourse-cdn.com/getpostman/original/3X/1/9/197387e5f93ee715bd62a8a8db199fb3c6875325.jpeg)

I try to get the variable with:

pm.collectionVariables.get(“createdSegmentId”) //i’m not sure if it should be with or without quotes

I try to use it later with:

pm.expect((jsonData.data.createLp.segmentId)).to.equal(createdSegmentId) // i’ve tried with and without {{}} around variable name

In the end I get an error:

ReferenceError: createdSegmentId is not defined

 ![fail](https://us1.discourse-cdn.com/getpostman/original/2X/a/a034c8257ea4267cf632449ed26b70345e6cf4f2.jpeg)

thanks for any help

---

<div class="post-metadata">

**Author:** ![sean.keegan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sean.keegan/32/6353_2.png) [@sean.keegan](https://community.postman.com/u/sean.keegan)\
**Post date:** [April 12, 2021, 8:11pm UTC](https://community.postman.com/t/using-collection-variable-in-test/22538/2 "2021-04-12T20:11:27Z")

</div>

Hi @timothy.jahn! Welcome to the Postman forum and community! 🏆

The issue here is that you have used the `pm.collectionVariables.get` method, but you didn’t actually retrieve that value and assign it to a local variable. So in line 16, there’s no local variable actually called `createdSegmentId`. If you change line 14 to say the following, it should work:

`let createdSegmentId = pm.collectionVariables.get("createdSegmentId");`

Another option would be to avoid setting a local variable. To do that, you can retrieve the value of the collection variable directly in line 16 with the following:

`pm.expect((jsonData.data.createLp.segmentId).to.equal(pm.collectionVariables.get("createdSegmentId")));`

Hope that helps!
