How to "set a Global Variable" (random) only once and use them for all the keys In the collection

I have a request body as follows, Here I need the keys “id”, “sys2Acct” and “shortName” to be set to a same random value.

{"id":"0372","name":"AN","phone":"","sys2Acct":"7491","shortName":"2592", etc..}

NOTE: the request body has huge number of key’s, I somehow want to assign the same number (i.e random) to the mentioned keys in this request and also for the other requests in the collection.

Currently I have written the following script in Collection Pre-Request, But the problem is it is being called every time for the keys, hence resulting in different numbers for the keys

Code in collection pre-request script:

pm.globals.set("randomNumber", JSON.stringify('{{$randomCreditCardMask}}'));

Request scripts:

"id":{{randomNumber}},"name":"AN","contName":"CN","phone":"","sys2Acct":{{randomNumber}

Output: All the keys are getting different numbers.

{"id":"0372","name":"AN","contName":"CN","phone":"","sys2Acct":"7491","shortName":"2592"}

Any help appreciated :slight_smile:

Can you please guide me how to achieve this.