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â)();
In the body of the test -
âamountâ: {{amount}},
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)