Deprecated warning in the Postman Console

I’m starting getting the Warning in the Console

“Using “postman.setNextRequest” is deprecated. Use “pm.execution.setNextRequest()” instead.”

But it doesn’t look like I use postman.setNextRequest command in Scripts code.

Hey @osiuser :wave:

Could you provide more details about the script that you have where you’re seeing this in the console?

async function getExternalAccountId(account_id, i) {
    //Get the external_account_Id
    await pm.sendRequest({
        url: pm.environment.get("baseUrl") + '/restapi/v2.1/accounts/' + account_id,
        method: 'GET',
        header: {
            "User-Agent": "PostmanRuntime",
            "Connection": "keep-alive",
            "Authorization": "Bearer " + pm.environment.get("accessToken")
        },
        //body: { }
    }, async function (err, res) {
        if (err) {
            console.log(err);
        } else {
            var jsonAccountData = res.json();
            var s = (i == 0) ? '' : ('_' + i);
            await postman.setEnvironmentVariable("externalAccountId" + s, jsonAccountData.externalAccountId);
            //await console.log("externalAccountId" + s, jsonAccountData.externalAccountId);
        }
    }
    );
}


async function processResponseBody(responseBody) {

    var jsonData = JSON.parse(responseBody);
    var accounts = jsonData.accounts;
    if (jsonData.hasOwnProperty("sub")) {
        await postman.setEnvironmentVariable("userId", jsonData.sub);
    }

    await postman.setEnvironmentVariable("email", jsonData.email);


    for (var i = 0; i < accounts.length; i++) {
        if (accounts[i].hasOwnProperty("is_default")) {
            if (accounts[i].is_default === true) {
                if (accounts[i].hasOwnProperty("organization")) {
                    await postman.setEnvironmentVariable("organizationId", accounts[i].organization.organization_id);
                }
                await postman.setEnvironmentVariable("baseUrl", accounts[i].base_uri);
                await postman.setEnvironmentVariable("accountId", accounts[i].account_id);

                await postman.setEnvironmentVariable("externalAccountId", '');
                //Get the external_account_Id
                await getExternalAccountId(accounts[i].account_id, 0);

                await postman.setEnvironmentVariable("accountName", accounts[i].account_name);

                //console.log("Default", i, accounts[i].account_name, jsonAccountData.externalAccountId)

            }
        }

        await postman.setEnvironmentVariable("accountId" + '_' + (i + 1), accounts[i].account_id);

        await postman.setEnvironmentVariable("externalAccountId" + '_' + (i + 1), '');
        await getExternalAccountId(accounts[i].account_id, i + 1);

        await postman.setEnvironmentVariable("accountName" + '_' + (i + 1), accounts[i].account_name);
        await postman.setEnvironmentVariable("accountId_isDefault" + '_' + (i + 1), accounts[i].is_default);

        //await console.log(i, accounts[i].account_name);
    }
}


// Main function
//Get Authorization header value and save it in the var
let token=pm.request.headers.get('Authorization');
pm.environment.set("accessToken", token.replace('Bearer ',''));

postman.clearEnvironmentVariable("userId");
postman.clearEnvironmentVariable("email");
postman.clearEnvironmentVariable("accountId");
postman.clearEnvironmentVariable("externalAccountId");
postman.clearEnvironmentVariable("baseUrl");
postman.clearEnvironmentVariable("accountName");
postman.clearEnvironmentVariable("organizationId");
postman.clearEnvironmentVariable("organizationName");

var i = 1;
while (pm.environment.has('accountId' + '_' + i)) {
    postman.clearEnvironmentVariable("accountId" + '_' + i);
    postman.clearEnvironmentVariable("externalAccountId" + '_' + i);
    postman.clearEnvironmentVariable("accountId_isDefault" + '_' + i);
    postman.clearEnvironmentVariable("accountName" + '_' + i);
    i++
};



processResponseBody(responseBody);

There are many places that your would need to change the script which don’t relate to the error message.

// Old
postman.setEnvironmentVariable()

// New
pm.environment.set()
// Old
postman.clearEnvironmentVariable("userId")

// New
pm.environment.unset("userId")

It would be interesting to see if changing those, should the setNextRequest message in the console?

I’ve made the suggested changes.

There is no warning now, but the new code removed a lot variables in the Environment.

That’s completely my bad :cry:

I should have said .unset() and not clear() - I apologise if that’s messed things up for you.

My understanding was “unset” set value to Null, while "clear"removes the variable. I used “clear” in the code to remove a specific vars.

Why were other vars that were not used in the code removed?

Update:
I’ve just tested and can confirm that both postman.setEnvironmentVariable and postman.clearEnvironmentVariable cause the Warning to appear

Using the .unset('var_name') would remove that variable but .clear() would remove all the variables in that scope.

To remove a variable from the active environment, specifying the variable by name:

  • pm.environment.unset(variableName:String):function

To clear all variables in the active environment:

  • pm.environment.clear():function

I’ve also used a function in the past to clear out specific variables that start with a certain prefix, might not work in your context but worth sharing:

function cleanupEnvVars(prefix) {
    const clean = Object.keys(pm.environment.toObject())
    clean.forEach((arrItem) => {
        if (arrItem.startsWith(prefix)) {
            pm.environment.unset(arrItem)
        }
    })
}

cleanupEnvVars('test_');

That could also be added to the Package Library and used anywhere, could even be amended to point to any of the scopes.

Thanks Danny. It helps.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.