Postman- Data Driven Test - Function to generate 100 different Values and setting up in collection variable

Hi Support team,
Can you please help me on creating a function for data driven test and need to be passed in the collection variable in each iteration.

A Pre-request script creates unique value need to be generated using time stamp and time stamp need to suffix to the predefined text
eg -1:
abc1-timestamp
xyz2-timestamp

/Generates time stamp/
var moment = require(‘moment’);
var current_unixtimestamp = moment.utc().valueOf();
pm.collectionVariables.set(“current_unixtimestamp”, moment.utc().valueOf());
console.log(current_unixtimestamp);

var moment = require(‘moment’);
pm.sendRequest(‘http://worldtimeapi.org/api/timezone/America/danmarkshavn’, function(err, res) {
var startdate = moment.utc().utcOffset(res.json().utc_offset).format(“YYYY-MM-DDTHH:mm:ss”);
console.log(‘GMT start date&time’);
pm.collectionVariables.set(‘startdate’, startdate);
console.log(startdate);

var istartdate = moment.utc().utcOffset(res.json().utc_offset).add(1, 'month').startOf('month').format('YYYY-MM-DDTHH:mm:ss');
console.log('GMT Next month startdate');
pm.collectionVariables.set('istartdate', istartdate);
console.log(istartdate);

var ienddate = moment.utc().utcOffset(res.json().utc_offset).add(1, 'month').format("YYYY-MM-DDTHH:mm:ss");
pm.collectionVariables.set('ienddate', ienddate);
console.log('GMT End date&time');
console.log(ienddate);
});

/* Generates unique value and suffixing to the pre-defined text */
const itext1 = ‘abc’;
pm.collectionVariables.set(‘itextval’, itext1 .valueOf() + pm.collectionVariables.get(‘current_unixtimestamp’));
var i1 = pm.collectionVariables.get(‘itextval’);
console.log(i1 );

You’ve included some code but haven’t really said what problem you are having.

What bit do you need help with? What’s not working?

Need your help in creating a function which will generate different unique value based on timestamp and need to suffix to text. for every iteration and it need to be passed to the variable
eg-
abc12345678
xyz099766666
fgh97777339

What do you want the timestamp to look like and are there any rules about the suffix?

You have a start and end date in your code. Which one are we interested in? Or do you just mean the current timestamp?

I really don’t recommend having collection and local variables with the same name (the startdate in your code). The scope can get messy.

The following is an example (in a small loop) that mixes the lodash library to generate a random prefix from a list and Moment. (Lodash is built-in so you don’t need to declare it).

moment = require("moment");
for (let i = 0; i < 10; i++) {
    prefix = ["ABC","DEF","GHI","JKL","MNO","PQR","STU","VWX"];
    console.log(_.sample(prefix) + moment().format("YYYYMMDD"));
};

image

You can see with the small number of prefixes that there are duplicates. (So how random do you want this to be?). You might need to include the minutes and second in the date to ensure its uniqueness.

Thanks for the reply