Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner Script to validate Custom Number Field

Ingo Förster January 8, 2024

I try to validate a custom number field. I tried this insde the transistion from create->open

Everytime I try to create the issue I get an error:
````

"errorMessages": [ "Jira expression failed to parse: line 1, column 5:\n=, =>, ., ?., [, ?.[, (, *, /, %, +, -, <, <=, >, >=, ==, !=, &&, ||, ??, ?, ; or EOF expected, targetField encountered." ],

````

 

Maybe I have missed something with the following script?

````

def targetField = getCustomFieldValue("Target")
def val = targetField.getValue() as Number
def regexFormat = /^\d+$/
if(val ==~ regexFormat){
targetField.clearError()
return true
} else {
targetField.setError("Required Target must be positive integer")
return false
}

````

2 answers

1 vote
Kristian Walker _Adaptavist_
Community Champion
January 8, 2024

Hi Ingo,

I can confirm that inside Jira cloud, validators use the Jira Expression Framework provided by Atlassian.

Your code will not work as you must use the syntax for the Jira Expression Framework in your validator.

We have some examples in our docs page here that show this syntax. They will help you create the validator that you require. 

I hope this helps.

Regards,

Kristian

0 votes
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 8, 2024

Welcome to the Atlassian Community!

I don't know where you got this code from, but it is very wrong

Line 2 tries to cast a string to a number with the "as Number" statement, so the variable will contain nothing unless the string really is just a number.

Line 3 tries to set a regex pattern into a variable, but does not specify a valid string, it contains a load of control characters, which is probably why you are seeing the "unexpected end of file" error.

Line 4 has an invalid comparison ( you need to decide whether you mean "equals" or "like", "equals like" is total nonsense.   Or combine them with a "or" statement: if x == y OR x is like y)

And, in general, you are trying to specify a regex string to check the value, but you're not actually trying the regex on the string.  == and ~ are not regex comparisons.

I suspect line 3 could be causing the script error you are seeing (but it does look like you may not have given us all of it, so it might be another line we have not seen)

A good example of how to use a regex in a script is at https://library.adaptavist.com/entity/use-regex-to-find-emails-address

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events