# Else if condition not working

**URL:** <https://discuss.elastic.co/t/else-if-condition-not-working/274383>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [May 28, 2021, 11:00pm UTC](https://discuss.elastic.co/t/else-if-condition-not-working/274383 "2021-05-28T23:00:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sandeep\_Raju](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@Sandeep\_Raju](https://discuss.elastic.co/u/Sandeep_Raju)\
**Post date:** [May 28, 2021, 11:00pm UTC](https://discuss.elastic.co/t/else-if-condition-not-working/274383/1 "2021-05-28T23:00:52Z")

</div>

Hi all,

I'm trying to write a scripted field.  
But I"m unable to execute else if condition as below:

```auto
if (doc.containsKey("report_card.grade") && !doc["report_card.grade"].empty) { 
if (doc["report_card.grade"].value == 5) { return "A"; } 
else if 
(doc["report_card.grade"].value == 4) { return "B"; }
else if 
(doc["report_card.grade"].value == 3) { return "C"; } 
else if 
(doc["report_card.grade"].value == 2) { return "D"; } 
else if 
(doc["report_card.grade"].value == 1) { return "E"; } 
else if 
(doc["report_card.grade"].value == 0) { return "F"; } 

else { return "-" ; } } 
else { return "-" ; }

```

I have tried condition with \>= or \<=, still it displays only 1 value(A or E)  
I'm trying to figure out where i went wrong in code.  
But unable to do so.

Any help please.

---

<div class="post-metadata">

**Author:** ![Sandeep\_Raju](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@Sandeep\_Raju](https://discuss.elastic.co/u/Sandeep_Raju)\
**Post date:** [May 29, 2021, 3:31pm UTC](https://discuss.elastic.co/t/else-if-condition-not-working/274383/2 "2021-05-29T15:31:53Z")

</div>

OK, so I came to know my field is a list.

```auto
report_card
{
  "name": "antiVirus",
  "grade": 1
},
{
  "name": "localFirewall",
  "grade": 1
},
{
  "name": "insecureListeningPorts",
  "grade": 5
},
{
  "name": "failedLogin",
  "grade": 1
},
{
  "name": "networkVulnerabilities",
  "grade": 5
},
{
  "name": "systemAging",
  "grade": 5
},
{
  "name": "supportedOS",
  "grade": 4
}

```

So I need to use a different Script for a list type field using a 'for loop' or have to get my fields seperately instead of a list

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 26, 2021, 3:31pm UTC](https://discuss.elastic.co/t/else-if-condition-not-working/274383/3 "2021-06-26T15:31:54Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
