I have searched for the solution and found multiple answers but didn’t work for me.
Under Collection Folder/Parent folder → Under Pre-request script ..
[Note - env_amount is created under Collection Folder → Variables tab]
function getAmount()
{ const amount= (“random_number”, _.random(1, 10));
pm.environment.set(“env_amount”,amount);
};
In the later folders/ tests - need to get this amount in ‘Body’ request. So I have added the following stmt to Pre-request of that test:
var amount=pm.collectionVariables.get(“getAmount”)();
Your function is trying to set an environment variable, where in the pre-request script you have it trying to retrieve a collection variable that doesn’t appear to be declared anywhere.
The best way to declare a function is to define it as a global variable (don’t use LET, VAR or CONST)