How to segregate response based on the inputdata and passing the response to next request as input

Requirement : Trying to pass Request1 output(particular field only) as input to Request2
Based upon the test data i am getting different response for request1. where i need help how to pass based on the condition
Basically for Positive flow and Negative flow response code is coming as 200 only.
I need to manipulate based on the response body.

Sample Response Body (Req1)

{
“Sample1Header”:[
{
“requireddata” : “needtopassthisto next”
“data 2” : “data2”
]
}

Sample Response Body (Req1)

{
“Sample2”:[
{
“Message”: “No Record.”
}
]
}

So if i get the Sample1Header i need to pass the “requireddata” filed to next request
Currently i am storing this “requireddata” as env variables. Positive flow is working If i give condition

Code I have tried

var requireddata = “”;
var jsonData = JSON.parse(responseBody);
pm.globals.set(“jsonData”,jsonData);

if(requireddata = jsonData.Sample1Header[0].requireddata){
console.log(requireddata);
pm.globals.set(“requireddata”,jsonData.Sample1Header[0].requireddata);}
else{
(pm.expect(pm.response.text()).to.include(“No Record”))
{
requireddata = null;
pm.global.set(“requireddata”,requireddata);
console.log(“No value from first response”);
}}

Tip #1, don’t call your global variable the same as a local variable.

Give them different names. It will use the local scope first. (Plus it can get confusing).

Use const to declare the response, as the response should be constant (and not change).

Tip #2

Write your steps\logic out. It will you you, and others to troubleshoot.

I’m still trying to work out what you are trying to achieve.

You are physically setting the local variable requireddata to null, so as far as I can tell, your IF statement will always trigger the ELSE (unless the requireddata field in the actual response is actually null).

If you trying to craft positive and negative tests, then these should be separate tests. (Best practice and all that).

If you post code, can you please use the preformatted code, as its hard to read when everything is aligned to the left.

Sure we can help you on this, but just need to you refine your question and maybe split your code into steps so we can understand your logic better.

The following has a basic IF statement to show if an element exists or not. Not sure if this is what you are after.

// step 1 - parse response
// const jsonData = pm.response.json()

const jsonData = {
"Sample1Header":[
    {
        "requireddata" : "needtopassthistonext",
        "data 2" : "data2"
    }]
}

// step 2 - store requireddata into a local variable
var actualResult = jsonData.Sample1Header[0].requireddata;
console.log(actualResult);

// step 3 - if statement test 
// If it isn't "undefined" and it isn't "null", then it exists.

if(typeof(actualResult) != 'undefined' && actualResult != null) {
    console.log('Element exists!');
} else {
    console.log('Element does not exist!');
}

@ mdjones - Thanks a lot for your response and directions.
Issue got resolved by applying the below code.

if(!jsonData.Sample1Header)
{

#Actions need to perform
}
else
{

Code for the actions

}

Hi do you mind send the code that you apply for this scenario, I have a the same query, please

Thank you
Patricia