If else condition is not working. All the time running the else condition even the if is true
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2022 08:12 AM
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.
- Labels:
-
Discovery
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2022 08:24 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2022 08:27 AM
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");
}
}
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2022 01:54 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2022 01:58 PM
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!