# Painless script "Cannot cast from \[boolean\] to \[java.lang.String\]" issue

**URL:** <https://discuss.elastic.co/t/painless-script-cannot-cast-from-boolean-to-java-lang-string-issue/318034>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [November 2, 2022, 5:39pm UTC](https://discuss.elastic.co/t/painless-script-cannot-cast-from-boolean-to-java-lang-string-issue/318034 "2022-11-02T17:39:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex\_sws](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@alex\_sws](https://discuss.elastic.co/u/alex_sws)\
**Post date:** [November 2, 2022, 5:39pm UTC](https://discuss.elastic.co/t/painless-script-cannot-cast-from-boolean-to-java-lang-string-issue/318034/1 "2022-11-02T17:39:56Z")

</div>

Having trouble spotting the error in my script.

```auto
"reason": "class_cast_exception: Cannot cast from [boolean] to [java.lang.String]."

```

The Scripted field:

```auto
Instant Currentdate = Instant.ofEpochMilli(new Date().getTime());
long Fakedate = 956851031000L;
Instant Duedate = doc['duedate'].empty ? Instant.ofEpochMilli(Fakedate) : Instant.ofEpochMilli(doc['duedate'].value.getMillis());
Instant removed_date = doc['removed_date'].empty ? Instant.ofEpochMilli(Fakedate) : Instant.ofEpochMilli(doc['removed_date'].value.getMillis());

if (doc['duedate'].value == null || !doc.containsKey('duedate') || doc['duedate'].empty) { 
    return false;
    
} else if (Currentdate.isAfter(Duedate) && doc['Status'].value.contains('Open')){
    return true;
    
} else if (removed_date.isAfter(Duedate) && doc['Status'].value.contains('Closed')){
    return true;
    
} else {
    return false;
    
}

```

Here is the response:

```auto
{
  "took": 108,
  "timed_out": false,
  "terminated_early": false,
  "num_reduce_phases": 0,
  "_shards": {
    "total": 6,
    "successful": 0,
    "skipped": 0,
    "failed": 4,
    "failures": [
      {
        "shard": 0,
        "index": "",
        "node": "",
        "reason": {
          "type": "script_exception",
          "reason": "compile error",
          "script_stack": [
            "... '].empty) { \r\n return false;\r\n \r\n} else if ( ...",
            " ^---- HERE"
          ],
          "script": "Instant Currentdate = Instant.ofEpochMilli(new Date().getTime());\r\nlong Fakedate = 956851031000L;\r\nInstant Duedate = doc['duedate'].empty ? Instant.ofEpochMilli(Fakedate) : Instant.ofEpochMilli(doc['duedate'].value.getMillis());\r\nInstant removed_date = doc['removed_date'].empty ? Instant.ofEpochMilli(Fakedate) : Instant.ofEpochMilli(doc['removed_date'].value.getMillis());\r\n\r\nif (!doc.containsKey('duedate') || doc['duedate'].empty) { \r\n return false;\r\n \r\n} else if (Currentdate.isAfter(Duedate) && doc['Status'].value.contains('Open')){\r\n return true;\r\n \r\n} else if (removed_date.isAfter(Duedate) && doc['Status'].value.contains('Closed')){\r\n return true;\r\n \r\n} else {\r\n return false;\r\n \r\n}",
          "lang": "painless",
          "position": {
            "offset": 450,
            "start": 425,
            "end": 475
          },
          "caused_by": {
            "type": "class_cast_exception",
            "reason": "class_cast_exception: Cannot cast from [boolean] to [java.lang.String]."
          }
        }
      },
      {
        "shard": 0,
        "index": "",
        "node": "",
        "reason": {
          "type": "script_exception",
          "reason": "compile error",
          "script_stack": [
            "... '].empty) { \r\n return false;\r\n \r\n} else if ( ...",
            " ^---- HERE"
          ],
          "script": "Instant Currentdate = Instant.ofEpochMilli(new Date().getTime());\r\nlong Fakedate = 956851031000L;\r\nInstant Duedate = doc['duedate'].empty ? Instant.ofEpochMilli(Fakedate) : Instant.ofEpochMilli(doc['duedate'].value.getMillis());\r\nInstant removed_date = doc['removed_date'].empty ? Instant.ofEpochMilli(Fakedate) : Instant.ofEpochMilli(doc['removed_date'].value.getMillis());\r\n\r\nif (!doc.containsKey('duedate') || doc['duedate'].empty) { \r\n return false;\r\n \r\n} else if (Currentdate.isAfter(Duedate) && doc['Status'].value.contains('Open')){\r\n return true;\r\n \r\n} else if (removed_date.isAfter(Duedate) && doc['Status'].value.contains('Closed')){\r\n return true;\r\n \r\n} else {\r\n return false;\r\n \r\n}",
          "lang": "painless",
          "position": {
            "offset": 450,
            "start": 425,
            "end": 475
          },
          "caused_by": {
            "type": "class_cast_exception",
            "reason": "class_cast_exception: Cannot cast from [boolean] to [java.lang.String]."
          }
        }
      }
    ]
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

```

---

<div class="post-metadata">

**Author:** ![matw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matw/32/13913_2.png) [@matw](https://discuss.elastic.co/u/matw)\
**Post date:** [November 3, 2022, 8:45am UTC](https://discuss.elastic.co/t/painless-script-cannot-cast-from-boolean-to-java-lang-string-issue/318034/2 "2022-11-03T08:45:58Z")

</div>

Hi and welcome to our community  
So you are using a scripted field and not a runtime field, right (second option is the recommended way btw). Did you set the type of your field to boolean?

Thx & best,  
Matthias

 ![New_Scripted_Field_-Kibana_Sample_Data_Logs-_Elastic](https://us1.discourse-cdn.com/elastic/original/3X/d/6/d6f23add867defbc372a540f8a9a8c1b093c9040.png)

---

<div class="post-metadata">

**Author:** ![alex\_sws](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@alex\_sws](https://discuss.elastic.co/u/alex_sws)\
**Post date:** [November 3, 2022, 4:11pm UTC](https://discuss.elastic.co/t/painless-script-cannot-cast-from-boolean-to-java-lang-string-issue/318034/3 "2022-11-03T16:11:07Z")

</div>

yes, the type is boolean. what's the difference between the two?

---

<div class="post-metadata">

**Author:** ![alex\_sws](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@alex\_sws](https://discuss.elastic.co/u/alex_sws)\
**Post date:** [November 3, 2022, 6:03pm UTC](https://discuss.elastic.co/t/painless-script-cannot-cast-from-boolean-to-java-lang-string-issue/318034/4 "2022-11-03T18:03:55Z")

</div>

Yes it is, see below:

 ![sssss](https://us1.discourse-cdn.com/elastic/original/3X/6/8/686f13cde177105740f961418a0b00c5ae8e0d55.png)

Any other ideas? Version is 7.9.3.

Also, This is done for a specific visual in a kibana dashboard, how would you accomplish this with the least impact to performance?

---

<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:** [December 1, 2022, 6:04pm UTC](https://discuss.elastic.co/t/painless-script-cannot-cast-from-boolean-to-java-lang-string-issue/318034/5 "2022-12-01T18:04:18Z")

</div>

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