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

Hi @Supriya25 

If you're using only Object.keys(result.errors).length > 0 in an if condition:

You can safely use either of these. Both are functionally identical.


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

 

Best Regards
Nitish Saini

k_lutz
Tera Expert

Hi,

 

I think the issue is that you may need close the "object.keys" code after the "+" sign. I think it may be interpretting it in correctly unless you have that piece of code properly defined. I did use a tool to rewrite it the code...not sure if this is more than what you may need. Attaching the file in case you want to try it.

 

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

 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) )