Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,361,135
Community Members
 
Community Events
168
Community Groups

How to restrict components field based on another field.

Hi.

I have been working on component field restriction. I was not able to find much documentation, so I want to share my final product for review.

// restrict components fields based on another single select field
import com.atlassian.jira.bc.project.component.ProjectComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

import static com.atlassian.jira.issue.IssueFieldConstants.COMPONENTS

@BaseScript FieldBehaviours fieldBehaviours

def projectComponentManager = ComponentAccessor.getProjectComponentManager()

def components = projectComponentManager.findAllForProject(issueContext.projectObject.id)
def availableComponents = []

def issueTypeField = getFieldById(COMPONENTS)

def productField = getFieldByName("Product")
def productOption = productField.getValue() as String

if ( productOption == "Infinity Fabric") {
availableComponents.addAll(components.findAll { it.name in ["Application"] })
}
issueTypeField.setFieldOptions(availableComponents)

 

I am going to have multiple IF options. Can you suggest how to make it more elegant with an options map?

Thanks.

 

1 answer

Anyway this script is working.

// restrict components fields based on another single select field
import com.atlassian.jira.bc.project.component.ProjectComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

import static com.atlassian.jira.issue.IssueFieldConstants.COMPONENTS

@BaseScript FieldBehaviours fieldBehaviours

def projectComponentManager = ComponentAccessor.getProjectComponentManager()

def components = projectComponentManager.findAllForProject(issueContext.projectObject.id)
def availableComponents = []

def issueTypeField = getFieldById(COMPONENTS)

def productField = getFieldByName("Product")
def productOption = productField.getValue() as String

if ( productOption == "Infinity Fabric") {
availableComponents.addAll(components.findAll { it.name in ["Application"] })
}
issueTypeField.setFieldOptions(availableComponents)

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Online AMA this week: Your project management questions answered by Jira Design Lead James Rotanson

We know that great teams require amazing project management chops. It's no surprise that great teams who use Jira have strong project managers, effective workflows, and secrets that bring planning ...

201 views 1 6
Read article

Atlassian Community Events