# Can you edit Collection Variables in a Pre-request script?

**URL:** <https://community.postman.com/t/can-you-edit-collection-variables-in-a-pre-request-script/14800>\
**Category:** Help Hub\
**Tags:** variables, collections, post, javascript\
**Created:** [August 6, 2020, 7:55pm UTC](https://community.postman.com/t/can-you-edit-collection-variables-in-a-pre-request-script/14800 "2020-08-06T19:55:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ghosty.USB](https://avatars.discourse-cdn.com/v4/letter/g/4af34b/32.png) [@Ghosty.USB](https://community.postman.com/u/Ghosty.USB)\
**Post date:** [August 6, 2020, 7:55pm UTC](https://community.postman.com/t/can-you-edit-collection-variables-in-a-pre-request-script/14800/1 "2020-08-06T19:55:32Z")

</div>

**I’ve put the Important information in bold just to make it easier to skim read.**

This may sound strange, but I have a situation where someone I’m working with is using only **collection variables** as **integer** storage. They are using the variables to store integers, and need to **edit them** in a **pre-request script** , then **update the variable** so the value is saved for next use.

I’ve tried a couple basic JavaScript fixes but I can seem to get the variables to post the correct number & update it in the collections.

**For reference** , this is being used for discord webhooks to store numbers in an easy to read format

**Example of situation:**  
Collection Variable: ‘money’ = 500  
Post request with the content of {{money}} sends “500” to the discord channel

Is there a way to do something like `pm.collectionVariables.set('money', 'money'-200)` ?

---

<div class="post-metadata">

**Author:** ![Ghosty.USB](https://avatars.discourse-cdn.com/v4/letter/g/4af34b/32.png) [@Ghosty.USB](https://community.postman.com/u/Ghosty.USB)\
**Post date:** [August 6, 2020, 8:01pm UTC](https://community.postman.com/t/can-you-edit-collection-variables-in-a-pre-request-script/14800/2 "2020-08-06T20:01:03Z")

</div>

P.S. I’m much more of a C# user and don’t know much about JavaScript’s formatting, so if possible, in any solution ideas, example formatting would also be a great help.

---

<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:** [August 6, 2020, 8:07pm UTC](https://community.postman.com/t/can-you-edit-collection-variables-in-a-pre-request-script/14800/3 "2020-08-06T20:07:56Z")

</div>

Would something like this work?

```auto
let money = parseInt(pm.collectionVariables.get('money')) 

pm.collectionVariables.set('money', money - 200)

```

Not been able to run this yet so I’m just giving you a quick idea 😁

---

<div class="post-metadata">

**Author:** ![Ghosty.USB](https://avatars.discourse-cdn.com/v4/letter/g/4af34b/32.png) [@Ghosty.USB](https://community.postman.com/u/Ghosty.USB)\
**Post date:** [August 6, 2020, 8:25pm UTC](https://community.postman.com/t/can-you-edit-collection-variables-in-a-pre-request-script/14800/4 "2020-08-06T20:25:46Z")

</div>

> [@danny-dainton](#):
>
> ```auto
> let money = parseInt(pm.collectionVariables.get('money')) 
> 
> pm.collectionVariables.set('money', money - 200)
> 
> ```

This seems to come back with an interesting result…  
on the first run, it come up as “null”  
but on a second run, it successfully posted “300” (initial 500 subtract the 200) and updated the variable correctly.

It may have just been a project issue that it came up as “null” on the first run but it’s all working now! This was a much more simple fix than what I was trying.

Thanks a ton.  
I can’t believe I didn’t think of using parseInt haha.
