postmanpostman-collection-runnerdata-filespostman-pre-request-script

How to set environment variable using a data file in Postman


I have a data file having a column userid, now i want to set a environment variable based on the value of userid using pre-request script.

var i = data.userId;
console.log(i);

if(i==='1')
{
    pm.environment.set("id","i");
    //console.log(pm.environment.get("id"));
}

Can someone please help

Solution

  • I would have thought that you would need to adjust the script to something like this:

    var i = pm.iterationData.get("userId");
    console.log(i);
    
    if( i === '1' )
    {
        pm.environment.set("id", i);
       //console.log(pm.environment.get("id"));
    }