How can I check in a behaviour script that the "Affects version/s" field is empty?

Gergely Fehér April 9, 2020

I have an "Affects Branch/es" custom field, and I'd like to force to have only one of "Affects version/s" and "Affects Branch/es" filled - in our system filling none or both is invalid.

A tried the following code, but it does not work:

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

@BaseScript FieldBehaviours fieldBehaviours

def branchesField = getFieldByName("Affects Branch/es")
def branchesFieldStringValue = branchesField.getValue()

def versionsField = getFieldById("versions")
def versionsFieldStringValue = versionsField.getValue()

if (branchesFieldStringValue == null && versionsFieldStringValue == null) {
branchesField.setError("brancherror both empty 3")
}

if (branchesFieldStringValue != null && versionsFieldStringValue != null) {
branchesField.setError("brancherror both filled 4")
}


I think the problem is the way I checked if they're empty or not. I tried with converting them with .tostring() and comparing with "" and some other ways - but now I think I'm getting lost. Can someone help me?

1 answer

1 accepted

1 vote
Answer accepted
SITTER_Adrien
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.
April 10, 2020

Hello @Gergely Fehér

 

This is a Groovy script. Groovy is made to simplify this kind of things.

Try this:

if (branchesFieldStringValue && versionsFieldStringValue) {
branchesField.setError("branchesboth empty 3")
}

if (!branchesFieldStringValue && !versionsFieldStringValue) {
branchesField.setError("brancherror both filled 4")
}

 Regards,

 

Adrien

Gergely Fehér April 12, 2020

Hi @SITTER_Adrien,

Thanks a lot, it works!

Siva Indukuri December 28, 2020

I wrote the below code in behaviour script to check if the field is empty . It is working if it is empty.. however, If i enter something the error is not cleared. Please help ,

I need to clear the error message if there is something in the field.

def loc=getFieldById("customfield_10612")
if (loc) {
loc.setError("Please Enter loc")
}

Siva Indukuri December 28, 2020

How do we check if the given field value is text or not in behaviour script in jira

Suggest an answer

Log in or Sign up to answer