# Runtime field component mapping with IF in script fails to parse due to compile error

**URL:** <https://discuss.elastic.co/t/runtime-field-component-mapping-with-if-in-script-fails-to-parse-due-to-compile-error/345816>\
**Category:** Elasticsearch\
**Tags:** runtime-fields\
**Created:** [October 26, 2023, 12:45pm UTC](https://discuss.elastic.co/t/runtime-field-component-mapping-with-if-in-script-fails-to-parse-due-to-compile-error/345816 "2023-10-26T12:45:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmgeurts](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dmgeurts/32/95115_2.png) [@dmgeurts](https://discuss.elastic.co/u/dmgeurts)\
**Post date:** [October 26, 2023, 12:45pm UTC](https://discuss.elastic.co/t/runtime-field-component-mapping-with-if-in-script-fails-to-parse-due-to-compile-error/345816/1 "2023-10-26T12:45:52Z")

</div>

I'm trying to create a mapping based on a value. However, whatever I try to save the component template, I'm shown the following error:

> ⚠ Unable to create component template  
> Failed to parse mapping: compile error

The runtime field component mapping I'm trying to create is:

```auto
if (!doc['policy_evaluated.spf'].value.equals('None') && 
  doc['policy_evaluated.spf'].value.toLowerCase().equals("pass"))
{
  return true
} else {
  return false
}

```

In examples of scripts with if statement I see them wrapped in `"""`, but with or without, I get the same error.

---

<div class="post-metadata">

**Author:** ![dmgeurts](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dmgeurts/32/95115_2.png) [@dmgeurts](https://discuss.elastic.co/u/dmgeurts)\
**Post date:** [October 26, 2023, 12:51pm UTC](https://discuss.elastic.co/t/runtime-field-component-mapping-with-if-in-script-fails-to-parse-due-to-compile-error/345816/2 "2023-10-26T12:51:54Z")

</div>

One must use emit not return... Correct script:

```auto
if (!doc['policy_evaluated.spf'].value.equals('None') &&
  doc['policy_evaluated.spf'].value.toLowerCase().equals("pass"))
{
  emit(true)
} else {
  emit(false)
}

```

---

<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:** [November 23, 2023, 12:52pm UTC](https://discuss.elastic.co/t/runtime-field-component-mapping-with-if-in-script-fails-to-parse-due-to-compile-error/345816/3 "2023-11-23T12:52:27Z")

</div>

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