Capture Json guid value and save in different variable for each iteration

I know i can add same Json request to capture value of itemlist again and save it diff variables.

Is there another way to run same request in multiple iteration, instead overwriting same Env variable it should save itemlist (since its different each time) with different variables like for var2, var3 so on…

Run same request in iteration and save itemid[i] into Variable_1,Variable_2 etc for Itemid1, itemid2…so on respectively

for e.g like For 5 iteration in can save 5 env variable with same run via newman on postmanrunner?

Token Generated
ItemId -1(captured itemid and saved it in Variable_1) 
Saved it in variable_1

If I understand you correctly, you want to run the same request multiple times, and grab a unique value and save it to a new variable each time the request is run?

You could do something like this:

let iterationCount = pm.collectionVariables.get('iterationCount');
if(iterationCount) {
  iterationCount = Number(iterationCount);
}
else {
  iterationCount = 1;
}

const jsonData = pm.response.json();
const value = jsonData.value; // this would be whatever your property you want

let values = pm.collectionVariables.get('values');
if(values) {
  values = JSON.parse(values);
}
else {
  values = [];
}

values.push(value);
pm.collectionVariables.set('values', JSON.stringify(values));

iterationCount++;

if(iterationCount <= 5) { // This would be your threshold for how many times to loop
  pm.collectionVariables.set('iterationCount', iterationCount);
  postman.setNextRequest('Name of this request');
} 
else {
  pm.collectionVariables.unset('iterationCount');
}

Yes @allenheltondev, i am looking the same thing. Let me run and comeback to you.
i have updated the value and request name. There is too much of “value” text used :stuck_out_tongue: getting confused

@allenheltondev seems like it did run 5 times but getting error on declaration part. Also variables were not added in env. i used this code in test script.
image

After bit cleaning, i am not getting ReferenceError: Cannot access ‘jsonData’ before initialization

Can you post the updated script?

I had the variables set to collection variables, not environment variables. You’ll have to change that in the javascript if you want it to be set to an environment variable.

1 Like

environment changed to from collection

let iterationCount = pm.environment.get('iterationCount');
if(iterationCount) {
  iterationCount = Number(iterationCount);
}
else {
  iterationCount = 1;
}
const jsonData = pm.response.json();
//const jsonData = JSON.parse(responseBody);
const value = jsonData.itemid; // updated with required path
let values = pm.environment.get('values');
if(values) {
  values = JSON.parse(values);
}
else {
  values = [];
}
values.push(value);
pm.environment.set('values', JSON.stringify(values));
iterationCount++;
if(iterationCount <= 5) { // This would be your threshold for how many times to loop
  pm.environment.set('iterationCount', iterationCount);
  postman.setNextRequest('GenerateItem');
} 
else {
  pm.environment.unset('iterationCount');
}

updated only unique itemid path from response and the environment getting only 1 value that too i think of 2nd iteration not like “112503877”

image

@allenheltondev. Thank but the requirement is iteration = 5 and values are saved in variable like: i used these item id separately must not used same itemid again
image

No problem, you can just create the variables dynamically:

let iterationCount = pm.environment.get('iterationCount');
if(iterationCount) {
  iterationCount = Number(iterationCount);
}
else {
  iterationCount = 1;
}
const jsonData = pm.response.json();
pm.environment.set(`item_${iterationCount+1}`,  jsonData.itemid);
iterationCount++;
if(iterationCount <= 5) { // This would be your threshold for how many times to loop
  pm.environment.set('iterationCount', iterationCount);
  postman.setNextRequest('GenerateItem');
} 
else {
  pm.environment.unset('iterationCount');
}
1 Like

yeah pretty close, missed the 3rd

image

Are you sure that value exists in the API response?
I’d recommend adding a test in there.

let iterationCount = pm.environment.get('iterationCount');
if(iterationCount) {
  iterationCount = Number(iterationCount);
}
else {
  iterationCount = 1;
}
const jsonData = pm.response.json();
pm.test('Response has itemId', function(){
  pm.expect(jsonData).to.have.property('itemid');
});

pm.environment.set(`item_${iterationCount+1}`,  jsonData.itemid);
iterationCount++;
if(iterationCount <= 5) { // This would be your threshold for how many times to loop
  pm.environment.set('iterationCount', iterationCount);
  postman.setNextRequest('GenerateItem');
} 
else {
  pm.environment.unset('iterationCount');
}
1 Like

yes Allen, item dies exists in response. as guided Item test is pass and returning. below
image

This time it generated only 1 itemid

Some problem in postman.setNextRequest('generateitem');
if(iterationCount <= 8)
image
image
have to manually send same request.

I want to make sure you’re testing this by running it through the collection runner.

I just copied this exact script into my workspace and ran it through the runner without issues. it iterated 5 times and created 5 environment variables.

1 Like

Opps,:stuck_out_tongue: Sorry i ran it seperately. but when i ran it with collection it only make 3 env variables. There was some problem in my collection. let me move this and ran again…hopefully it will work.

Just one thing @allenheltondev, will same work with newman too ? i know its silly :smiley:

Yes it will. It should work as long as you aren’t sending it as a one-off single request.

1 Like

No i won’t :D, it will be collection. Thanks and Sorry i made you double check the code :stuck_out_tongue:

1 Like

Don’t worry about it, I’m happy to help.