# Converting scripted field to runtime field

**URL:** <https://discuss.elastic.co/t/converting-scripted-field-to-runtime-field/335571>\
**Category:** Kibana\
**Tags:** painless, runtime\
**Created:** [June 8, 2023, 8:37pm UTC](https://discuss.elastic.co/t/converting-scripted-field-to-runtime-field/335571 "2023-06-08T20:37:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ccloutr.alex](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ccloutr.alex/32/111402_2.png) [@ccloutr.alex](https://discuss.elastic.co/u/ccloutr.alex)\
**Post date:** [June 8, 2023, 8:37pm UTC](https://discuss.elastic.co/t/converting-scripted-field-to-runtime-field/335571/1 "2023-06-08T20:37:14Z")

</div>

Our stack was updated from version 7.9.3 to version 7.17.9 and I am having to convert some of the scripted fields to the new "runtime" field as it shows scripted fields as depreciated. It looks like some of the time functions have changed and i am having a hard time converting. How can i accomplish the same with a runtime field?

```auto
Instant CurDate = Instant.ofEpochMilli(new Date().getTime());
long FakeDate = 956851031000L;
Instant Ddate = doc["d_date"].empty ? Instant.ofEpochMilli(FakeDate) : Instant.ofEpochMilli(doc["d_date"].value.getMillis());
Instant Rdate = doc["r_date"].empty ? Instant.ofEpochMilli(FakeDate) : Instant.ofEpochMilli(doc["r_date"].value.getMillis());

if (doc["d_date"].value == null || !doc.containsKey('d_date') || doc["d_date"].empty) {
    return false;
 } else if (CurDate.isAfter(Ddate) && doc['status'].value.contains('Open')) {
    return true
 } else if (Rdate.isAfter(Ddate) && doc['status'].value.contains('Closed')) {
    return true;
 } else {
    return false;
 }

```

---

<div class="post-metadata">

**Author:** ![Marco\_Liberati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marco_liberati/32/82953_2.png) [@Marco\_Liberati](https://discuss.elastic.co/u/Marco_Liberati)\
**Post date:** [June 9, 2023, 1:00pm UTC](https://discuss.elastic.co/t/converting-scripted-field-to-runtime-field/335571/2 "2023-06-09T13:00:14Z")

</div>

Hi @ccloutr.alex

runtime fields need to `emit()` the result instead of returning.  
Try with this:

```auto
Instant CurDate = Instant.ofEpochMilli(new Date().getTime());
long FakeDate = 956851031000L;
Instant Ddate = doc["d_date"].empty ? Instant.ofEpochMilli(FakeDate) : Instant.ofEpochMilli(doc["d_date"].value.getMillis());
Instant Rdate = doc["r_date"].empty ? Instant.ofEpochMilli(FakeDate) : Instant.ofEpochMilli(doc["r_date"].value.getMillis());

if (doc["d_date"].value == null || !doc.containsKey('d_date') || doc["d_date"].empty) {
    emit(false);
 } else if (CurDate.isAfter(Ddate) && doc['status'].value.contains('Open')) {
    emit(true);
 } else if (Rdate.isAfter(Ddate) && doc['status'].value.contains('Closed')) {
    emit(true);
 } else {
    emit(false);
 }

```

---

<div class="post-metadata">

**Author:** ![ccloutr.alex](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ccloutr.alex/32/111402_2.png) [@ccloutr.alex](https://discuss.elastic.co/u/ccloutr.alex)\
**Post date:** [June 12, 2023, 6:33pm UTC](https://discuss.elastic.co/t/converting-scripted-field-to-runtime-field/335571/3 "2023-06-12T18:33:58Z")

</div>

yes - i have tried this - the field just ends up empty - nothing in it.

---

<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:** [July 10, 2023, 6:34pm UTC](https://discuss.elastic.co/t/converting-scripted-field-to-runtime-field/335571/4 "2023-07-10T18:34:39Z")

</div>

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