null condition check is not working in javascript

I have added one condition in JS. If one value in the incoming request payload coming as null then it will set a flow variable's value in that variable. But that condition for null checking is not working.

It is returning 500 error.

var jPayload = JSON.parse(getPayload);

if(jPayload.systemId)

{

var systemId= jPayload.systemId;

if((jPayload.systemId === "0") || (jPayload.systemId === " ") || (jPayload.systemId === null))

{ systemId = var1;

}else

{ systemId= systemId;

}

}

Note: systemId is a key in incoming json payload.

0 10 1,828
10 REPLIES 10

Not applicable

it seems if(jPayload.systemId) is coming as false. If the value is null then the first if is only false, so nothing will execute in the braces.

try using if(jPayload.systemId == null)

Hi Priyadarshi,

Request payload is not coming as false. I am sending JSON payload with the field

"systemId" : null

and I have tried the approach you have mentioned.

But still I am getting 500.

Not applicable

I am able to replicate the error. The issue is in the parsing of the object. It requires to be stringified.

The error code is something as below

var getPayload = {"systemId":null, "email":"abc@gmail.com"};



var jPayload = JSON.parse(getPayload);


if(jPayload.systemId)


{


var systemId= jPayload.systemId;


if((jPayload.systemId === "0") || (jPayload.systemId === " ") || (jPayload.systemId === null))


{ systemId = var1;


}else


{ systemId= systemId;


}


}

The corrected one is as below

var getPayload = {"systemId":null, "email":"abc@gmail.com"};

//This is the new addition
getPayload = JSON.stringify(getPayload);


var jPayload = JSON.parse(getPayload);


if(jPayload.systemId)


{


var systemId= jPayload.systemId;


if((jPayload.systemId === "0") || (jPayload.systemId === " ") || (jPayload.systemId === null))


{ systemId = var1;


}else


{ systemId= systemId;


}


}

after below code added the parsing is resolved.

//This is the new addition
getPayload = JSON.stringify(getPayload);

Appreciate your prompt response. But no luck.
I am still getting the same error after stringify the payload. Other two condition is working fine. But for the null condition check I am getting 500.

{"fault":{"faultstring":"Unresolved variable : EnquiryId","detail":{"errorcode":"entities.UnresolvedVariable"}}}

I don't see "EnquiryId" variable in your script. So, you are getting this error. This is a different error. You can share your full JS-code.

I referred EnquiryId as systemId. Actually both are same.

Below my JS code:

var getPayload = context.getVariable('request.content');

var EnquiryId = context.getVariable("apigee.EnquiryId");

var CIDirectEnquiryId = context.getVariable("apigee.CIDirectEnquiryId");

var ResponseStatus = "Received";

var DateTime = context.getVariable("dateTime");

try{ getPayload = JSON.stringify(getPayload);

// Parse the request payload

var jPayload = JSON.parse(getPayload);

if(jPayload.EnquiryId)

{

var EnquiryId = jPayload.EnquiryId;

if((jPayload.EnquiryId === "0") || (jPayload.EnquiryId === " ") || (jPayload.EnquiryId === null))

{

EnquiryId = CIDirectEnquiryId;

}else

{

EnquiryId = EnquiryId;

}

}

}

catch(err)

{

errorFlag="true";

}

context.setVariable("EnquiryId",EnquiryId); context.setVariable("CIDirectEnquiryId",CIDirectEnquiryId); context.setVariable("Status","Received"); context.setVariable("DateTime",DateTime);

looks like the below line is creating error.

var EnquiryId = context.getVariable("apigee.EnquiryId");

can you try to print this? I feel this is not available. I could reproduce the 500 error when the value is not avialbel.

apigee.EnquiryId This variable is available. I am extracting this variable in a extract variable policy before this JS policy. And I can see it is printing the value.

can you try step by step print for the variable? I am not able to replicate the error. In the step by step print, you can see where the error is showing.

I don't see any specific lines causing the error you see, also the try/catch block should mean that any error between those lines should be caught ie not interrupt execution. Have you run a trace, do you know which policy is triggering this error?

With that said, the code still has a number of issues, redundant lines and redundant logic..

1. to make your code more readable, follow conventions of using camelcase for variable names, use meaningful names for variables - dont mix method names with variables names, indent..

2. if (jPayload.EnquiryId) is already going to evaluate for null/empty string, so the later null check and empty string check are redundant. The only other check that I believe isnt redundant is for whitespace but is that necessary for your use case? Also, this means that EnquiryId would only ever get set to CIDirectEnquiryId if EnquiryId is " "

3. EnquiryId is being set to itself ie this doesnt do anything.

5. You stringify the payload only to parse it again on the next line