why Object.keys(myObject).length > 0 always returning FALSE

Supriya25
Tera Guru

Hi All,

I request you kindly help me on below issue.
we are trying to handle the error from ECC Queue PayLoad

var gr = new GlideRecord("ecc_queue");
gr.addQuery('sys_id', 'record sysID');
gr.query();
if(gr.next()){
var payloadxml= gr.payload.toString();
var objXML = new global.XMLDocument(payloadxml);
var xmloutput = objXML.getNodeText('//output').trim();
if(JSUtil.notNil(xmloutput)){
var result;
result=JSON.parse(xmloutput);
gs.info(" Result is : "+result.errors);// OUTPUT : [object object]
}
}

 

gs.info("Result has Length : "+result.errors.length);//OUTPUT: 1
gs.info("Result has Length : "+Object.keys(result.errors).length);//OUTPUT: 1
gs.info("Result has Length : "+Object.keys(result.errors).length>0);//OUTPUT: FALSE
it has to return TRUE, why it is returning as FALSE ?

Please help me on this.  

1 ACCEPTED SOLUTION

Hi @Supriya25 

Correct format for using it inside an if condition:
if (!!result.errors && (Object.keys(result.errors).length > 0)) {
// Your logic here
}

 

Please accept answer and mark this as helpful if this answers your question.

 

Best Regards
Nitish Saini

View solution in original post

7 REPLIES 7

Nitish Saini
Tera Expert

Hi Supriya,

Use parentheses to make sure the logical comparison happens before the concatenation:
gs.info("Result has Length : " + (Object.keys(result.errors).length > 0));

OR 
You can try this as well:- 
var hasErrors = Object.keys(result.errors).length > 0;
gs.info("Result has errors: " + hasErrors);

Please mark this as helpful and accept answer if this answers your question.

 

Best Regards
Nitish Saini

Hi @Nitish Saini 
Object.keys(result.errors).length > 0 I would like to use in IF-CONDITTION

so which format I have to use
if( !!result.errors && Object.keys(result.errors).length > 0 ) 
Or 
if( !!result.errors && (Object.keys(result.errors).length > 0) )

Hi @Supriya25 

Correct format for using it inside an if condition:
if (!!result.errors && (Object.keys(result.errors).length > 0)) {
// Your logic here
}

 

Please accept answer and mark this as helpful if this answers your question.

 

Best Regards
Nitish Saini

when we are considering  with other conditions also we took (Object.keys(result.errors).length > 0) in separate ().  

But how about if I want to check only Object.keys ?
if(
Object.keys(result.errors).length > 0 ) 
Or 
if((Object.keys(result.errors).length > 0) )