# Painless Script Error, Creating New Variable on Data View

**URL:** <https://discuss.elastic.co/t/painless-script-error-creating-new-variable-on-data-view/333599>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [May 16, 2023, 7:58pm UTC](https://discuss.elastic.co/t/painless-script-error-creating-new-variable-on-data-view/333599 "2023-05-16T19:58:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marscar](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@marscar](https://discuss.elastic.co/u/marscar)\
**Post date:** [May 16, 2023, 7:58pm UTC](https://discuss.elastic.co/t/painless-script-error-creating-new-variable-on-data-view/333599/1 "2023-05-16T19:58:43Z")

</div>

Hello! I am trying to create a new variable on a Data View using this painless script, but when trying to save, I am getting the generic error "Invalid Painless Script". The error also tells be to fix the highlighted errors, but there are no highlighted errors. Here is the script in question

```auto
if (doc.containsKey("ProcessCrash_split")){ 
    def processCrash = doc["ProcessCrash_split"].value; 
    int commaIndex = processCrash.lastIndexOf(","); 
    if (commaIndex > 0){ 
        emit(processCrash.substring(commaIndex+1));
        return;
    } 
    emit(processCrash);
    return;  
} 
emit("noCrash");
return;

```

Note: \* this script works on the dev tools query editor, replacing "emit(\*\*\*)" with "return \*\*\*"

 ![Screenshot 2023-05-16 at 12.59.23 PM](https://us1.discourse-cdn.com/elastic/original/3X/5/3/53e66295ccdc1821604fc9f24410f1d39afbfee3.jpeg)

---

<div class="post-metadata">

**Author:** ![Priscilla\_Parodi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/priscilla_parodi/32/43047_2.png) [@Priscilla\_Parodi](https://discuss.elastic.co/u/Priscilla_Parodi)\
**Post date:** [May 17, 2023, 3:59pm UTC](https://discuss.elastic.co/t/painless-script-error-creating-new-variable-on-data-view/333599/2 "2023-05-17T15:59:42Z")

</div>

Hello Mark,

The script you pasted here and the one in the image are different. Try using exactly what you pasted here.

```auto
if (doc.containsKey("ProcessCrash_split")){ 
    def processCrash = doc["ProcessCrash_split"].value; 
    int commaIndex = processCrash.lastIndexOf(","); 
    if (commaIndex > 0){ 
        emit(processCrash.substring(commaIndex+1));
        return;
    } 
    emit(processCrash);
    return;  
} 
emit("noCrash");
return;

```

---

<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:** [June 14, 2023, 4:00pm UTC](https://discuss.elastic.co/t/painless-script-error-creating-new-variable-on-data-view/333599/3 "2023-06-14T16:00:38Z")

</div>

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