Generating multiples of a variable within the same POST message

Hi folks, I’ve been using Postman for a couple of years now but I’m only now just delving into creating my own Pre-Request scripts.
I’m using a .json POST request to send data into a SQL database to represent a parcel that contains multiple items. The parcel has its own reference number, but each item needs its own unqiue customer demand reference as well as a unique invoice number.

Previously I would send each customer demand individually, but with the same parcel reference hard-coded, so each time I sent the POST message a new Invoice Number and DemandID would be generated, which was fine. However I now want to be able to create whole parcels at once, so each POST request contains multiples of the same payload within the .json, which means my RNGs are no longer fit for purpose because they generate a random number but then insert the same one into each instance of the payload.

In the Pre-Request script I currently have:

pm.collectionVariables.set(“InvoiceNumber”, _.random(10000,99999))

pm.collectionVariables.set(“demandID”, _.random(29000000000,29999999999))

So my question is, is there a way for the Pre-Request script to generate a different invoice number/demand ID multiple times within the same message without resorting to:

pm.collectionVariables.set(“InvoiceNumber1”, _.random(10000,99999))
pm.collectionVariables.set(“InvoiceNumber2”, _.random(10000,99999))
pm.collectionVariables.set(“InvoiceNumber3”, _.random(10000,99999))

I could do it this way, but it kind of defeats the point of the time I’m trying to save.

Hopefully I’ve explained this as best as I can!
Thanks in advance.

When you say, “time you are trying to save”. What do you mean?

Setting three environment variables with random data like this should take milliseconds and looks like a perfectly valid way of achieving what you want. Sometimes the simplest way is the best.

I can’t think of any easier way to do this. If you put the command into some sort of loop or function if that is what you are talking about, it would still be at least 3 lines of code.

Not sure if any of the Dynamic Variables are of any use to you.

It uses the Faker JavaScript library, but the options seem limited.