# Kibana Scripted Fields aggregation

**URL:** <https://discuss.elastic.co/t/kibana-scripted-fields-aggregation/328779>\
**Category:** Kibana\
**Tags:** dashboard\
**Created:** [March 29, 2023, 6:49am UTC](https://discuss.elastic.co/t/kibana-scripted-fields-aggregation/328779 "2023-03-29T06:49:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sam\_sawant](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sam_sawant/32/119107_2.png) [@sam\_sawant](https://discuss.elastic.co/u/sam_sawant)\
**Post date:** [March 29, 2023, 6:49am UTC](https://discuss.elastic.co/t/kibana-scripted-fields-aggregation/328779/1 "2023-03-29T06:49:07Z")

</div>

I am new to kibana and trying to create dashboard. I have 4 scripted fields which are calculating dollar savings for specific process due to automation. i am trying to get sum of these 4 scripted fields to show total savings due to process automation. I looked at documentation but unable to find way to achieve this. Some fields may have null value for certain record. i have tried following code but its not working.

i have tried to assign following code to new scripted field to get sum for 2 fields.

```auto
def field1_value = params['_source']['HR_COST_AV'];
def field2_value = params['_source']['Marketing_Cost_AV'];

if(field1_value != null) {
  if(field2_value != null) {
    return field1_value + field2_value;
  } else {
    return field1_value;
  }
} else {
  return field2_value;
}

```

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [March 29, 2023, 4:11pm UTC](https://discuss.elastic.co/t/kibana-scripted-fields-aggregation/328779/2 "2023-03-29T16:11:33Z")

</div>

> [@sam\_sawant](#):
>
> `field1_value = params['_source']['HR_COST_AV'];`

Hi,

in elasticsearch document, there are no empty field. Those fields without any value just do not exist.  
Therefore you have to check null before you access `params['_source']['HR_COST_AV']`

See "Missing fields" section of the below document. In addition, access a field using doc values should be preferreble for performance reason.

> **[Accessing document fields and special variables | Elasticsearch Guide \[8.11\]...](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-fields.html#modules-scripting-doc-vals)**

---

<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:** [April 26, 2023, 4:11pm UTC](https://discuss.elastic.co/t/kibana-scripted-fields-aggregation/328779/3 "2023-04-26T16:11:55Z")

</div>

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