How to check if apigee flow variable is empty or null in javascript

Not applicable

How to check if apigee flow variable is empty or null in javascript? For example, here I am creating flow variable called screeningReturn from xml payload.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="EVExtractScreeningResults">
    <DisplayName>EV.ExtractScreeningResults</DisplayName>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <Source clearPayload="false">screeningReturnResponse</Source>
    <XMLPayload stopPayloadProcessing="false">
        <Namespaces/>
        <Variable name="screeningReturn" type="nodeset">
            <XPath>/Results</XPath>
        </Variable>
    </XMLPayload>
</ExtractVariables>

The variable screeningReturn can be null if xml has don't have Results tag. I need to check this in java script. I am using below function to do null or empty check.

 function isEmpty (data) {
	    
	    if(typeof(data) == 'number' || typeof(data) == 'boolean') {
            return false;
        }
        if(typeof(data) == 'undefined' || data === null ) {
            return true;
        }
  
        if(typeof(data.length) != 'undefined') {
            return data.length == 0;
        }
		for (var h in data) {
			if (data.hasOwnProperty(h)) {
				return false;
			}
		}
		return true;
    };

But I am getting following error - TypeError: Cannot find function hasOwnProperty in object com.apigee.flow.Variables@44a8ce54.

Solved Solved
1 6 8,027
1 ACCEPTED SOLUTION

Not applicable

@Sujnana Rai try this please.

function isEmpty(data) {

if (data) {

return true;

}

return false;

}

View solution in original post

6 REPLIES 6

@Sujnana Rai, here is the JS function to perform null check.

function isEmpty(obj) {

try {

if (typeof obj == 'object') {

if (Object.keys(obj).length > 0) {

return false;

} else {

return true;

}

} else {

if ((obj !== null) && (obj !== undefined) && (obj !== "")) {

return false;

} else {

return true;

}

}

} catch (err) {

return true;

}

}

function isNullOrEmpty(value) { //check for null if (value == null || value == undefined) { return true; } else if (!isNaN(value)) { return false; } else if (typeof value == "string" && value.length == 0) { // Check for String return true; } else if (typeof value == "object" && Object.keys(value).length == 0) { // Check for Objects and Arrays return true; } return false; }

Not applicable

If you are using JavaScript policy. In the request flow, you can add the condition before calling JavaScript.

<Step> 
  <Condition>NOT(screeningReturn = null)</Condition>
  <Name>JavaScript-Step-Name</Name>
</Step>

Not applicable

@Sujnana Rai try this please.

function isEmpty(data) {

if (data) {

return true;

}

return false;

}

This works! Short and simple!

Checking if the variable is non-null is relatively simple in JS.

var r = context.getVariable('screeningResults');
if (r) {
 // not empty, not null
}
else {
 // null or empty
}

But I guess what you want to do is further parse that nodeset. The problem is the nodeset you have extracted from the XML is not accessible from within a JS callout. So you will have to deal with that nodeset in some other way. Perhaps by an XSLT.

If you explain exactly what you want to do in more detail, we may be able to help further.