ScriptRunner Validation Problem

Pawel July 27, 2021

Hello,

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."
}

if(!b.isInteger()){
error = "Number incorrect."
}

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

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

 

2 answers

1 accepted

2 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 28, 2021

Hi @Pawel

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()
sample.clearError()

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:-

behaviour_config.png

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:-

test1.png

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:-

test2.png

test4.png

test3.png

 

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

Paul July 28, 2021

Thank you Ram!

It worked - thank you also for much cleaner code!

Thanks,
Paul

1 vote
John Chin
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.
July 28, 2021

Hi @Pawel ,

You can try remove this code:

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

and add into the else statement:

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

 I hope this helps.

Pawel July 28, 2021

Thanks Chin,

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

Thanks,
Paul

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events