Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root


1 badge earned


Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!


Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.


Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!


Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

ScriptRunner Validation Problem


I have a problem with text custom field's validation in ScriptRunner (Behaviours) in Jira Server.

Users should type Axxxx (e.g. A1234), so I check few things:

- the first character must be A
- the total length must be 5
- characters from 2 - 5 must be numbers

In general the code works well except for just 1 scenario.

When I enter an incorrect value, e.g. A123, then I get an error (expected). However, when I delete my entry leaving the field empty, the error persists. 

The field is optional.

It looks to me like ScriptRunner doesn't read the field's value when it is deleted (empty). It reads all the updates like adding or removing a character, but when the field is empty it remembers the last value.

Any idea how to fix this, please?

Here is the code:

import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
String field = getFieldByName("My Test Field").getValue()

def fc = getFieldById(getFieldChanged());
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField currentfc = customFieldManager.getCustomFieldObjectByName("My Test Field")
String currentfcString = (underlyingIssue?.getCustomFieldValue(currentfc))

if((fc?.value) != currentfcString) {
if(field !=""){

if(field.length() < 5 || field.length() > 5){
getFieldByName("My Test Field").setError("Expected A1234.")
else {
def a = field.substring(0,1)
def b = field.substring(1, 5)
String error = "N"

if(!(a =="A" )){
error = "Incorrect prefix, should be A."

error = "Number incorrect."

if(error != "N"){
getFieldByName("Change ID").setError(error)
getFieldByName("Change ID").clearError()

if(field ==null){
getFieldByName("My Test Field").clearError()


2 answers

1 accepted

2 votes
Answer accepted

Hi @Paul

For your requirement, you could try to modify your Behaviour code to something like:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def sample = getFieldById(fieldChanged)
def sampleValue = sample.value.toString()

if (sampleValue.length() > 0) {
if (sampleValue.length() >= 2 && sampleValue.length() <= 5) {
if (!sampleValue.startsWith("A")) {
sample.error = "Expected to Start with 'A'"

2.upto(5) {
if(sampleValue.length() == (it as int) && !sampleValue.charAt(it - 1 as int).digit) {
sample.error = "Characters from 2 to 5 can only be numbers"

} else {
sample.error = "Characters have not met the permitted length"

Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a print screen of the Behaviour configuration:-


Below are a few test print screens:-

1) If only one character is inserted, it complains that the characters entered have not met the permitted length as shown below:-


2) Once at least two characters have been entered, it validates the first character to see if it is the alphabet A and also the following characters to check if they are Numeric as shown in the images below:-





I hope this helps to answer your question. :)

Thank you and Kind Regards,


Thank you Ram!

It worked - thank you also for much cleaner code!


1 vote

Hi @Paul ,

You can try remove this code:

if(field ==null){
getFieldByName("My Test Field").clearError()

and add into the else statement:

if(field !="") { 
getFieldByName("My Test Field").clearError()

 I hope this helps.

Thanks Chin,

I've tried that, unfortunately it didn't work.


Suggest an answer

Log in or Sign up to answer

Atlassian Community Events