How to validate field depending on component selection (simple script validator)?

Jessica Schupp October 16, 2012

I have to set up a workflow transistion validation via simple script validator but it does not work. Depending on a value choosen in field "Component/s" another custom field hast be checked if it's empty or not.

I've tried different script strings like:

(issue.components.name.contains 'xy'||(issue.components.name.contains 'Umsystem'&&cfValues['Affected Umsystem-Release']!=null)

Each part of this simple script works as single validation but not in whole. Do you know why?

Additionally, how can I set the codition "component does not contain a certain value"? I've tried this:

result = issue.getComponents().find('Guide-Res') < 1

componentManager.issue.getComponents().find('Umsystem') < 1

issue.getComponents.find('Umsystem Guide-Res') < 1

Thanks for your help! It's very much appreciated!!!

Version of JIRA v4.0.2#472; Plugin Version: 1.7.13

7 answers

1 accepted

9 votes
Answer accepted
JamieA
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.
November 1, 2012

It should be

issue.components*.name.contains('...')

because Issue.getComponents returns a Collection. Note the * - the spread-dot operator. This calls getName on every element in the collection, returning a List.

Whether you use brackets or not as in Wolfgang's answer makes no odds, they're optional in groovy unless they make a syntactic difference.

Also the !=null in your question is redundant.

> Additionally, how can I set the codition "component does not contain a certain value"? I've tried this

! issue.components*.name.contains('...')

Rahul Savaikar June 26, 2018

Thanks a lot Jamie - this worked for me.

 

Regards,

Rahul

0 votes
AdamCliff June 17, 2020

Hello,

I was wondering if you can use a wildcard on the following code?

Instead of

issue.components*.name.contains('user_1')
issue.components*.name.contains('user_2')
issue.components*.name.contains('user_3')

I could use something like

issue.components*.name.contains('user_*')
0 votes
Tom Lister January 9, 2020

Checking a component in a Behaviour

import com.atlassian.jira.issue.IssueFieldConstants
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
// import org.apache.log4j.Logger

// def log = Logger.getLogger("com.cheil.logging")

@BaseScript FieldBehaviours fieldBehaviours
// log.info("@@@ " + IssueFieldConstants.COMPONENTS)
// log.info("@@@@ " + getFieldById(IssueFieldConstants.COMPONENTS).getValue())
// log.info("@@@@change " + getFieldById(getFieldChanged()))
def fieldChanged = getFieldById(getFieldChanged())

// log.info("@@@@vaklue " + fieldChanged.getValue().name)

if(fieldChanged.getFieldId().compareTo(IssueFieldConstants.COMPONENTS) == 0)
{
def division = getFieldById("customfield_12213")
String comp = fieldChanged.getValue().name
if (comp.contains("09) SEO")) {
division.setRequired(false) // set true for release
} else {
division.setRequired(false)
}
}
0 votes
Avinash October 31, 2018

Using Behaviour, we can do it but the server side script needs to be added under Components field 

 

import com.atlassian.jira.bc.project.component.ProjectComponent

def components = getFieldById(getFieldChanged())

def isLaptopPurchase = components.value?.any { ProjectComponent component ->
component.name in ['a', 'b'] //for multi options
//component.name == 'a' for single option
}

def laptopPurchase = getFieldByName("my field")

if (isLaptopPurchase) {
laptopPurchase.setRequired(true)
} else { 
laptopPurchase.setRequired(false)
}

0 votes
Lacey McDonnell October 19, 2015

@Jamie Echlin [Adaptavist]

Having similar issue... I am trying to add conditional in a split workflow a la https://answers.atlassian.com/questions/267139 

However, it's the JIRA Component/s field I am trying to make conditionally mandatory at Analysis Complete rather than a custom field; per this thread, I added the validator issue.components.name.contains('Policy') to the version of the workflow transition that should make custom field Z mandatory, and it's not making the field mandatory... Ideas?

JamieA
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.
October 20, 2015

Can you paste the full script or attach a screenshot?

Lacey McDonnell October 20, 2015

For the Conditions on the workflow transition that will make the field mandatory:

issue.components.name.contains('Policy')

for the conditions on the workflow transition that will not make the field mandatory

! issue.components.name.contains('Policy') 

 

These, again, are added as a script workflow function, simple scripted condition, checks script. that's all I've done aside from create a cloned transition for the purpose.

 

 

JamieA
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.
October 20, 2015

To make the another field mandatory if components has Policyt it would be: ! issue.components.name.contains('Policy') || cfValues["Some other field"] The field name in the form is just where the message is shown, it has no bearing other than that. There are several examples here that should help: https://scriptrunner.adaptavist.com/latest/jira/recipes/workflow/validators/simple-scripted-validators.html

Lacey McDonnell October 21, 2015

Adding ! lets all issues move forward without mandatory field. Removing ! stops all issues from moving forward unless mandatory field populated. Not pulling on Component value. ____________________________________________ Validators (2) Required fields: Analysis Hrs, Bug Source, Complexity, Dev Estimate Hrs, Netsuite CA Classification, QA Estimate Hrs, RCA Categories - BA, Release Notes, Severity — AND Script workflow function : Simple scripted validator : Checks script: ! issue.components.name.contains('Policy') || cfValues["Policy_2015"]

0 votes
Jessica Schupp November 6, 2012

Thanks a lot. It works very well!

0 votes
Wolfgang Fellner November 1, 2012

try

issue.components.name.contains('xy')

instead of

issue.components.name.contains 'xy'

it worked for me

JamieA
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.
November 1, 2012

What i said below is correct, brackets make no difference for the example above, however with the code in the question they may well make a logical difference. If you're not sure whether to use brackets, use them.

Ryan Gamo June 26, 2015

It did not work for me on the "create issue" initial transition validation. Anything I'm missing?

Suggest an answer

Log in or Sign up to answer