# Problem with new script

**URL:** <https://discuss.elastic.co/t/problem-with-new-script/337800>\
**Category:** Kibana\
**Created:** [July 6, 2023, 1:08pm UTC](https://discuss.elastic.co/t/problem-with-new-script/337800 "2023-07-06T13:08:12Z")\
**Posts on this page:** 15\
**Page:** 2

<div class="post-metadata">

**Author:** ![Stratoula\_Kalafateli](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stratoula_kalafateli/32/70923_2.png) [@Stratoula\_Kalafateli](https://discuss.elastic.co/u/Stratoula_Kalafateli)\
**Post date:** [July 7, 2023, 8:09am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/21 "2023-07-07T08:09:34Z")

</div>

Also add a space between the variable and the /

---

<div class="post-metadata">

**Author:** ![Valerija](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Valerija](https://discuss.elastic.co/u/Valerija)\
**Post date:** [July 8, 2023, 7:03am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/22 "2023-07-08T07:03:17Z")

</div>

Hello,

I made changes and am sending printscreen of an error. It seems that systems sees the totalGood as 0 for some reason.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/9/c/9cf249d3418b6610a31fb8540534f603edfa97be.png)

Thank you...sorry for not replying right away but max reply limit was reached yesterday...

---

<div class="post-metadata">

**Author:** ![Stratoula\_Kalafateli](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stratoula_kalafateli/32/70923_2.png) [@Stratoula\_Kalafateli](https://discuss.elastic.co/u/Stratoula_Kalafateli)\
**Post date:** [July 10, 2023, 6:02am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/23 "2023-07-10T06:02:39Z")

</div>

Hey, if at least one document in your index doesnt have value for the actual quantity or is 0 then it will fail. You can make a check and do the division if the value exists and is not 0

---

<div class="post-metadata">

**Author:** ![Valerija](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Valerija](https://discuss.elastic.co/u/Valerija)\
**Post date:** [July 10, 2023, 6:12am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/24 "2023-07-10T06:12:44Z")

</div>

I see...can you give me some hint on how to do that?

Thanks.

---

<div class="post-metadata">

**Author:** ![henry22](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henry22/32/113545_2.png) [@henry22](https://discuss.elastic.co/u/henry22)\
**Post date:** [July 11, 2023, 12:42pm UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/25 "2023-07-11T12:42:36Z")

</div>

It seems like the error gets thrown before it even executes the script?

---

<div class="post-metadata">

**Author:** ![Valerija](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Valerija](https://discuss.elastic.co/u/Valerija)\
**Post date:** [July 12, 2023, 7:15am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/26 "2023-07-12T07:15:22Z")

</div>

Hello,

I am not sure...this is what the error description says.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/3/0/30b4c1f70999110feda835d2c2b520f061d74f5e.png)

What your coworker suggested is to _make a check and do the division if the value exists and is not 0_ but I am not sure how to do that.

Can you help me out with this?

---

<div class="post-metadata">

**Author:** ![henry22](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henry22/32/113545_2.png) [@henry22](https://discuss.elastic.co/u/henry22)\
**Post date:** [July 12, 2023, 8:07am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/27 "2023-07-12T08:07:52Z")

</div>

Yes, the failure seems to be a division by zero, according to that last error message.  
Can you, just for testing, add a 1 to the second value?  
Or just print out the bare values and see if it is zero.

---

<div class="post-metadata">

**Author:** ![henry22](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henry22/32/113545_2.png) [@henry22](https://discuss.elastic.co/u/henry22)\
**Post date:** [July 12, 2023, 8:09am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/28 "2023-07-12T08:09:14Z")

</div>

Or try to divide it by a constant, like 2.

---

<div class="post-metadata">

**Author:** ![Valerija](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Valerija](https://discuss.elastic.co/u/Valerija)\
**Post date:** [July 12, 2023, 8:27am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/29 "2023-07-12T08:27:13Z")

</div>

is this what you meant?

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/8/b/8b3f68c1ecc3d7d8fc99d635ea461de2645eea19.png)

..if yes...then the error is that it cannot find totalGood...

---

<div class="post-metadata">

**Author:** ![henry22](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henry22/32/113545_2.png) [@henry22](https://discuss.elastic.co/u/henry22)\
**Post date:** [July 12, 2023, 9:17am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/30 "2023-07-12T09:17:34Z")

</div>

Yes. That is very odd, since returning TotalGood in your first example was working, correct?

Unfortunately, I can't find a good doco covering painless scripting. I find it rather painful instead!

---

<div class="post-metadata">

**Author:** ![Valerija](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Valerija](https://discuss.elastic.co/u/Valerija)\
**Post date:** [July 12, 2023, 9:34am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/31 "2023-07-12T09:34:09Z")

</div>

Yes, totalGood script works w/o any problems.

---

<div class="post-metadata">

**Author:** ![Stratoula\_Kalafateli](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stratoula_kalafateli/32/70923_2.png) [@Stratoula\_Kalafateli](https://discuss.elastic.co/u/Stratoula_Kalafateli)\
**Post date:** [July 12, 2023, 10:29am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/32 "2023-07-12T10:29:47Z")

</div>

@Valerija this one fails because you removed the definition of the totalGood. If you add this line again in your script then it wont fail.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/8/b/8b3f68c1ecc3d7d8fc99d635ea461de2645eea19.png)

About the check to see if there is null or zero it should be a simple if statement.

Something like

```auto
def totalGood = doc['actualQuantity'].value - doc['failureQuantity'].value;

if (doc['actualQuantity'].value != null || doc['actualQuantity'].value != 0) {
   return totalGood / doc['actualQuantity'].value; 
} else {
    // return what makes sense here if the doc['actualQuantity'] does not exist
    return totalGood;
}

```

My script might have typos, please ensure that I have the fields names correctly

---

<div class="post-metadata">

**Author:** ![Valerija](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Valerija](https://discuss.elastic.co/u/Valerija)\
**Post date:** [July 12, 2023, 11:40am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/33 "2023-07-12T11:40:39Z")

</div>

Hello @Stratoula_Kalafateli thank you so much for having patience with me...very very much. I am totally new to this tool and even simple if statement in Kibana is new to me...

Just to let you know that the script works now...I changed just one thing in the solution you provided and it works:

def totalGood = doc['actualQuantity'].value - doc['failureQuantity'].value;  
if (doc['actualQuantity'].value != null && doc['actualQuantity'].value != 0) {  
return totalGood / doc['actualQuantity'].value;  
} else {  
return null;  
}

thank you once again a lot! 🙂

---

<div class="post-metadata">

**Author:** ![Stratoula\_Kalafateli](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stratoula_kalafateli/32/70923_2.png) [@Stratoula\_Kalafateli](https://discuss.elastic.co/u/Stratoula_Kalafateli)\
**Post date:** [July 12, 2023, 11:49am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/34 "2023-07-12T11:49:08Z")

</div>

I am happy we made this work 🙂

---

<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:** [August 9, 2023, 11:49am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/35 "2023-08-09T11:49:54Z")

</div>

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

[Previous page](https://discuss.elastic.co/t/problem-with-new-script/337800.md?page=1)
