If else condition is not working. All the time running the else condition even the if is true

UDAY KIRAN REDD
Tera Contributor

 

var gr= new GlideRecord("xyz_Table");
gr.addEncodedQuery("soem filter");
gr.query();

while(gr.next()){
if(gr.u_csdm_action==1)
{
gs.print("Test Pass");
}
else
{
gs.print("Test Failed");
}
}

Only else condition is running even I have records which make if condition true still else is running.

 

 

 

 

4 REPLIES 4

Steven Parker
Giga Sage

I would log gr.u_csdm_action to see what the value is just prior to the If statement:

gs.info("CSDM Action Value: " + gr.u_csdm_action);

Then check the System Logs -> Script Log Statements and see what the value is prior to the If statement.


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven

Aman Kumar S
Kilo Patron

Try this:

Your code is failing since you are comparing on object type with number, when you just dot walk to a field you get the type object, while using getValue gives you string equivalent

var gr= new GlideRecord("xyz_Table");
gr.addEncodedQuery("soem filter");
gr.query();

while(gr.next()){
if(gr.getValue("u_csdm_action") == "1"){
   gs.print("Test Pass");
}
else{
   gs.print("Test Failed");
}
}
Best Regards
Aman Kumar

dmathur09
Kilo Sage
Kilo Sage

Hi Uday,

Can you check the backend choice values of u_csdm_action field. It might be some string value where you would be required to put like this if(gr.u_csdm_action== 'string')

Regards,

Deepankar Mathur

Allen Andreas
Administrator
Administrator

Hi,

As has been mentioned by someone above...you should get into the habit of always using getters and setters, especially in a while loop.

Meaning, always use getValue() and setValue() to protect yourself.

Getting the value in that manner will retrieve it in string and then you can compare as "1", again, as mentioned above.

Please mark reply as Helpful, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!