# 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:** 20\
**Page:** 1

<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 6, 2023, 1:08pm UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/1 "2023-07-06T13:08:12Z")

</div>

Hi there,

I created a simple new script and it works w/o problems:

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

Then I tried to create another one and this one does not work and I do not see why.

def goodinPercent = doc['totalGood'].value / doc['actualQuantity'].value; return goodinPercent;

Can you please help me out with this?

Thank you.

Valerija

---

<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 6, 2023, 3:00pm UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/2 "2023-07-06T15:00:28Z")

</div>

Hey, you cant reference a scripted field in another. You need to change your second script. Sometihng like:

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

def goodinPercent = totalGood/ doc['actualQuantity'].value; 

```

---

<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 7, 2023, 6:24am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/3 "2023-07-07T06:24:35Z")

</div>

Hello,

Thank you so much for quick reply. I was not aware of that rule - thank you.

Now I changed the script as you suggested but is still does not work.

def goodinPercent = 'totalGood' / doc['actualQuantity'].value;

Any idea what might be a problem? ☹

Thank you so much in advance.

Regards.  
Valerija

---

<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, 7:31am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/4 "2023-07-07T07:31:04Z")

</div>

I think my script has an error. Try to remove the return

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

def goodinPercent = totalGood/ doc['actualQuantity'].value; 
return goodinPercent;

```

---

<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 7, 2023, 7:42am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/5 "2023-07-07T07:42:26Z")

</div>

> [@Stratoula\_Kalafateli](#):
>
> ```auto
> def totalGood = doc['actualQuantity'].value - doc['failureQuantity'].value; 
> 
> def goodinPercent = totalGood/ doc['actualQuantity'].value; 
> return goodinPercent;
> 
> ```

I did the following and still does not work...I probably do not understand what I should do so I did printscreen for you to see.

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

```auto
type or paste code here

```

def totalGood = doc['actualQuantity'].value - doc['failureQuantity'].value;  
def goodinPercent = 'totalGood'/ doc['actualQuantity'].value;  
return goodinPercent;

---

<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, 7:44am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/6 "2023-07-07T07:44:43Z")

</div>

Do you see any failures?

---

<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 7, 2023, 7:45am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/7 "2023-07-07T07:45:48Z")

</div>

Yes, but it says that the script is invalid.

---

<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, 7:45am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/8 "2023-07-07T07:45:49Z")

</div>

aaaa I think the problem is that you are adding "" on the variable

```auto
def goodinPercent = totalGood/ doc['actualQuantity'].value;

```

should be totalGood without the quotes

---

<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 7, 2023, 7:46am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/9 "2023-07-07T07:46:47Z")

</div>

I tried...with and w/o and still no luck.... ☹

---

<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, 7:50am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/10 "2023-07-07T07:50:28Z")

</div>

You have to check the preview to see the errors. Are the fields that you are subtracting of the same type?

---

<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 7, 2023, 7:52am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/11 "2023-07-07T07:52:08Z")

</div>

Let me give you additonal info...

1. I have created script and it works  

2. then I wanted to create another script that would do calculation with data from TotalGood and actualQuantity

so, what I thought is that since the first script is created and working, then I need to put in the second script just

def goodinPercent = totalGood / doc['actualQuantity'].value;

...but no luck with anything I did so far...

---

<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 7, 2023, 7:52am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/12 "2023-07-07T07:52:39Z")

</div>

both are painless, if that is what you are referring to

---

<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, 7:53am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/13 "2023-07-07T07:53:34Z")

</div>

What type is the totalGood and what type is the doc['actualQuantity']?

There is this Get help with the syntax...

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

If you click it you will see which is the problem

---

<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 7, 2023, 7:58am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/14 "2023-07-07T07:58:46Z")

</div>

both are painless...

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/6/1/616f44b10896cd4e7e892f54ca11c94b68ff119a.png)

...I did check the link you mentioned but see no reasons for error....

---

<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 7, 2023, 7:59am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/15 "2023-07-07T07:59:25Z")

</div>

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

---

<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:01am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/16 "2023-07-07T08:01:57Z")

</div>

Painless is the language not the type. Here you are creating a number scripted field but you need to check the type of the fields you are you using inside your script. Can you send a screenshot of the preview errors?

---

<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 7, 2023, 8:04am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/17 "2023-07-07T08:04:20Z")

</div>

There is so much for me to learn! 😃 thank you for having patience with me...

here is the preview of the error...  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/d/8/d81fd73e8f3fa77ce60c6260174df89fd65fa5aa.png)

---

<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:06am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/18 "2023-07-07T08:06:30Z")

</div>

When you click the Get help with the syntax and preview the results of your script a flyout opens. I want a screenshot from the preview results tab

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

---

<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 7, 2023, 8:07am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/19 "2023-07-07T08:07:26Z")

</div>

OK, I see...sorry...

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

---

<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:08am UTC](https://discuss.elastic.co/t/problem-with-new-script/337800/20 "2023-07-07T08:08:50Z")

</div>

Remove the quotes from totalGood and send me the screenshot again.

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