Test status code and set variable

I have an API that gets a list of phone numbers. I am creating a test that if it sucessfully gets the list of phone numbers, the test should also store the first number in the list as the variable phone_number in the collection for the next test. I am not sure what I am doing wrong? I am not able to print phone_number to the console or see it set as a variable in the collections either.

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
    const response = pm.response.json()
    pm.collectionVariables.set("phone_number", response[0]);
});

console.log('{{phone_number}}');

Hi @mrjoli021, Welcome to the Community!

Since you are unable to see the variable also set, can you please check if you are using the correct JSON path to extract the values?

If possible can you please share the sample response to understand your question better?

You can’t use that {{...}} syntax in that way, in the sandbox.

You would need to use this to log that variable value:

console.log(pm.collectionVariables.get('phone_number'));

But as @pranavdavar mentioned, we’d still need to see the response body to know the structure and see if you’re setting that correctly.

Hello,

I finally got the information. I realized that in order to set the variable you have to declare it in the collection first. Once I did that I was able to set it and call it correctly. Thanks,

That would have been created with the .set() function in the script :thinking:

You wouldn’t need to add that as a Collection Variable first.

@dannydainton Even I too have the same understanding :thinking: