# Cast into script failing

**URL:** <https://discuss.elastic.co/t/cast-into-script-failing/1807>\
**Category:** Elasticsearch\
**Created:** [June 3, 2015, 7:52am UTC](https://discuss.elastic.co/t/cast-into-script-failing/1807 "2015-06-03T07:52:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bruteche](https://avatars.discourse-cdn.com/v4/letter/b/df705f/32.png) [@bruteche](https://discuss.elastic.co/u/bruteche)\
**Post date:** [June 3, 2015, 7:52am UTC](https://discuss.elastic.co/t/cast-into-script-failing/1807/1 "2015-06-03T07:52:14Z")

</div>

Hi all,

I'm writing a script for an upsert and i'm having a facing one issue on casting a field.

Basically my field is a String but when myString.isNumber() i would like to cast it Integer with the function Integer.parseInt in order to do a comparison with another field but i'm always receiving the following error :

error":"ElasticsearchIllegalArgumentException[failed to execute script]; nested: GroovyScriptExecutionException[GroovyCastException[Cannot cast object '70' with class 'java.lang.String' to class 'java.lang.Integer']]; ","status":400

Any idea on what can be the issue, here is a part of code :

```
		if ( "${Integer.parseInt(sub.status)}" < lower){
			lower = "${Integer.parseInt(sub.status)}";
			subId = sub.erpNumber;
		}
```

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [June 3, 2015, 9:51am UTC](https://discuss.elastic.co/t/cast-into-script-failing/1807/2 "2015-06-03T09:51:57Z")

</div>

I did not try out but I suspect lower is an Integer so `lower = "${Integer.parseInt(sub.status)}";`fails because of the enclosing quotes?

---

<div class="post-metadata">

**Author:** ![bruteche](https://avatars.discourse-cdn.com/v4/letter/b/df705f/32.png) [@bruteche](https://discuss.elastic.co/u/bruteche)\
**Post date:** [June 3, 2015, 9:54am UTC](https://discuss.elastic.co/t/cast-into-script-failing/1807/3 "2015-06-03T09:54:41Z")

</div>

thanks for your reply i have also tested directly with Integer.parseInt without success ☹

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [June 3, 2015, 10:01am UTC](https://discuss.elastic.co/t/cast-into-script-failing/1807/4 "2015-06-03T10:01:39Z")

</div>

Did you get the same error or a different one? Wouldn't this work?

```auto
		if (Integer.parseInt(sub.status) < lower){
			lower = Integer.parseInt(sub.status);
			subId = sub.erpNumber;
		}

```

---

<div class="post-metadata">

**Author:** ![bruteche](https://avatars.discourse-cdn.com/v4/letter/b/df705f/32.png) [@bruteche](https://discuss.elastic.co/u/bruteche)\
**Post date:** [June 3, 2015, 1:29pm UTC](https://discuss.elastic.co/t/cast-into-script-failing/1807/5 "2015-06-03T13:29:35Z")

</div>

Exactly the same.

---

<div class="post-metadata">

**Author:** ![bruteche](https://avatars.discourse-cdn.com/v4/letter/b/df705f/32.png) [@bruteche](https://discuss.elastic.co/u/bruteche)\
**Post date:** [June 4, 2015, 12:52pm UTC](https://discuss.elastic.co/t/cast-into-script-failing/1807/6 "2015-06-04T12:52:44Z")

</div>

Any idea from where it could come from ? by the way i'm on ES 1.4

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [June 4, 2015, 10:26pm UTC](https://discuss.elastic.co/t/cast-into-script-failing/1807/7 "2015-06-04T22:26:30Z")

</div>

Could you build a recreation of your issue using curl or Sense? (including index creation, adding documents, and running a query with this script)

---

<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 6, 2017, 12:09am UTC](https://discuss.elastic.co/t/cast-into-script-failing/1807/8 "2017-07-06T00:09:32Z")

</div>


