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

**URL:** <https://community.postman.com/t/postman-data-driven-test-function-to-generate-100-different-values-and-setting-up-in-collection-variable/41594>\
**Category:** Help Hub\
**Tags:** tests, variables\
**Created:** [November 29, 2022, 10:24pm UTC](https://community.postman.com/t/postman-data-driven-test-function-to-generate-100-different-values-and-setting-up-in-collection-variable/41594 "2022-11-29T22:24:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![automationtech7](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@automationtech7](https://community.postman.com/u/automationtech7)\
**Post date:** [November 29, 2022, 10:24pm UTC](https://community.postman.com/t/postman-data-driven-test-function-to-generate-100-different-values-and-setting-up-in-collection-variable/41594/1 "2022-11-29T22:24:16Z")

</div>

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](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 );

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [November 30, 2022, 2:00pm UTC](https://community.postman.com/t/postman-data-driven-test-function-to-generate-100-different-values-and-setting-up-in-collection-variable/41594/2 "2022-11-30T14:00:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![automationtech7](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@automationtech7](https://community.postman.com/u/automationtech7)\
**Post date:** [December 1, 2022, 5:21am UTC](https://community.postman.com/t/postman-data-driven-test-function-to-generate-100-different-values-and-setting-up-in-collection-variable/41594/3 "2022-12-01T05:21:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [December 1, 2022, 1:36pm UTC](https://community.postman.com/t/postman-data-driven-test-function-to-generate-100-different-values-and-setting-up-in-collection-variable/41594/4 "2022-12-01T13:36:48Z")

</div>

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).

```auto
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](https://us1.discourse-cdn.com/getpostman/original/3X/d/e/decbba7e6fc37b358d7a9e00908c00a35eb19f0c.png)

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.

---

<div class="post-metadata">

**Author:** ![automationtech7](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@automationtech7](https://community.postman.com/u/automationtech7)\
**Post date:** [December 20, 2022, 2:47pm UTC](https://community.postman.com/t/postman-data-driven-test-function-to-generate-100-different-values-and-setting-up-in-collection-variable/41594/6 "2022-12-20T14:47:00Z")

</div>

Thanks for the reply
