Retain the variable value across iterations using env variable

Team

I am facing a challenge while trying to use an arrayList variable. Tried setting this up in an env variable. It is not retaining the old values.

The arraylist is always holding the latest value. Looks like it is initializing the arraylist for every iteration. I want to retain and append yo the arrayList till the end of CSV iteration.

Here is my scenario. I need to print the key which is not working.
There are list of keys in the CSV file.

Steps:

  1. Iterating through the CSV file (2000 rows) to read the Key values - PASS

  2. Passing the key value from csv in the post body of the request {
    “key”: {{keys}}
    } - PASS

  3. Execute the request - PASS

  4. If the request for that iteration key is not working, pushing that missing key into an array
    missedContentList.push(pm.iterationData.get(‘keys’)); - PASS

  5. At the end of the iterations, print the arraylist - FAIL - It is printing only the last value instead of printing entire array
    I have copied my code snippet.


var resultObject;
var missedContentList = [];

pm.test("1. Response status is 200", function () {
    pm.response.to.have.status(200);
});
pm.test("2. Response should be okay to process", function () {
    pm.response.to.not.be.error;
    pm.response.to.not.have.jsonBody("error");
});
console.log("iteration count, current iteration = ", pm.info.iterationCount, pm.info.iteration);

try {
    resultObject = JSON.parse(responseBody);
    if (responseCode.code !== 200) {

        console.log("Content not found for the keys from csv = " + pm.iterationData.get('keys'));
        missedContentList.push(pm.iterationData.get('keys')); //This is always pushing the latest key
        pm.environment.set("mList", (missedContentList)); //Tried setting it here as well.
        console.log("Missing key = ", missedContentList);

    }

    if (pm.info.iterationCount - 1 === pm.info.iteration) {
        console.log("length of the missing content in if = ", missedContentList.length); // This is always 1
        pm.environment.set("mList", (missedContentList)); /set the value at the end of iteration
        m1 = pm.environment.get("mList");
        printMissingContent();

    }

}
catch (ex) {
    console.log("Unexpected response body: " + ex);
    console.log(responseBody);
    pm.test("FAIL - Empty / Invalid response", () => { throw new Error(ex.message) });
    return;
}

function printMissingContent() {
    m = pm.environment.get("mList");
    console.log("Length of missedContentList inside the function= ", m.length);
    console.log("List of all missing content ");
    //This is always printing the last key not all the missing keys
    console.log("=============================");
    console.log(m);

    return;
}

Thanks, Raje

Hey @rjayaram :wave:

Thanks for posting in our community!

I understand that you would like to retain missedContentList over the number of iterations (requests), is that correct?

I think it is not currently working as you are expecting since you set missedContentList = [] at the start of the script. This will initialise the array at each request. Slight modification as below might help :smiley:

var resultObject;
var missedContentList;
if (pm.info.iteration === 0) {
   missedContentList = []; 
   pm.environment.set("mList", (missedContentList))
} else {
   missedContentList = pm.environment.get('mList')
}

pm.test("1. Response status is 200", function () {
    pm.response.to.have.status(200);
});
pm.test("2. Response should be okay to process", function () {
    pm.response.to.not.be.error;
    pm.response.to.not.have.jsonBody("error");
});
console.log("iteration count, current iteration = ", pm.info.iterationCount, pm.info.iteration);

try {
    resultObject = JSON.parse(responseBody);
    if (responseCode.code !== 200) {

        console.log("Content not found for the keys from csv = " + pm.iterationData.get('keys'));
        missedContentList.push(pm.iterationData.get('keys')); //This is always pushing the latest key
        pm.environment.set("mList", (missedContentList)); //Tried setting it here as well.
        console.log("Missing key = ", missedContentList);

    }

    if (pm.info.iterationCount - 1 === pm.info.iteration) {
        console.log("length of the missing content in if = ", missedContentList.length); // This is always 1
        //pm.environment.set("mList", (missedContentList)); /set the value at the end of iteration
        m1 = pm.environment.get("mList");
        printMissingContent();

    }

}
catch (ex) {
    console.log("Unexpected response body: " + ex);
    console.log(responseBody);
    pm.test("FAIL - Empty / Invalid response", () => { throw new Error(ex.message) });
    return;
}

function printMissingContent() {
    m = pm.environment.get("mList");
    console.log("Length of missedContentList inside the function= ", m.length);
    console.log("List of all missing content ");
    //This is always printing the last key not all the missing keys
    console.log("=============================");
    console.log(m);

    return;
}
1 Like

Thanks a lot. It works now.

Hey @rjayaram!

Awesome! Thanks for letting me know :smiley:

Please feel free to reach out if you have any other questions :wink: