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()
}
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:-
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,
Ram
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.