Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Require custom field based on Component/s field using Scriptrunner Behavior

Robbie E Lee January 21, 2020

I would like to require a text field (or a single-select dropdown) based on the value selected in the Component/s field. (The custom fields are Acceptance Criteria and Code review required?)

I have:

  • created a behavior
  • mapped the behavior to a project and issue type
  • selected a workflow from the Guide Workflow dropdown
  • added the Component/s field
  • added a condition: Workflow Action: Create
  • added a server-side script:
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def componentField = getFieldByName("Component/s")
def acceptanceCriteriaField = getFieldByName("Acceptance Criteria")

String componentValue = componentField.getValue()

if (componentValue == "AI Platform") {
    acceptanceCriteriaField.setRequired(true)


else {
acceptanceCriteriaField.setRequired(false)
}

 I am unable to make this work and could use some advice.

1 answer

2 votes
Kian Stack Mumo Systems
Community Champion
April 30, 2020

Hello Robbie,

I think the issue is that component/s is an array, and so a direct "==" comparison won't work.

Try something like this:

 

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

def componentField = getFieldById("components")
def acceptanceCriteriaField = getFieldByName("Acceptance Criteria")

def componentValue = componentField.getValue() as List<ProjectComponent>
if (componentValue.any {it.name == "AI Platform"} ) {

acceptanceCriteriaField.setRequired(true)
}

else {
acceptanceCriteriaField.setRequired(false)
}

 

Suggest an answer

Log in or Sign up to answer