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

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) ?

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.

Would something like this work?

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 :grin:

1 Like

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.

1 Like