Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

filter component list based on issue type

I need help  setting up a behavior to  filter list of components available by issue type.

3 answers

1 accepted

1 vote
Answer accepted

Hint: A cascading select is not the right solution. 

You can use something like the following behaviour, as an initialiser IIRC:

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.bc.project.component.ProjectComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.ConstantsManager
import com.onresolve.jira.groovy.user.FormField

ProjectComponentManager projectComponentManager = ComponentAccessor.getProjectComponentManager()
ConstantsManager constantsManager = ComponentAccessor.getConstantsManager()

def pid = getIssueContext().getProjectId()
def components = projectComponentManager.findAllForProject(pid)
assert components*.name.containsAll(["Comp1", "Comp2", "Comp3"]) // must have these components

def Map issueTypeToComponents = [
    "Bug": ["Comp1", "Comp2"],
    "Task": ["Comp3"],
];

FormField formComponent = getFieldById("components")

def issuetypeId = issueContext.getIssueTypeId()

String issueType = constantsManager.getIssueTypeObject(issuetypeId).getName()

if (issueTypeToComponents.keySet().contains(issueType)) {
    List relevantComponents = issueTypeToComponents[issueType] as List
    def Map fieldOptions = [:]

    components.findAll { ProjectComponent pc ->
        relevantComponents.contains(pc.getName());
    }.each {ProjectComponent pc ->
        fieldOptions.put(pc.getId() as String, pc.getName())
    }

    formComponent.setFieldOptions(fieldOptions)
}

Getting the following error while trying to save behavior:

Compilation failure: startup failed: Script1.groovy: 29: unable to resolve class ProjectComponent @ line 29, column 20. }.each {ProjectComponent pc -> ^ Script1.groovy: 27: unable to resolve class ProjectComponent @ line 27, column 32. components.findAll { ProjectComponent pc -> ^ 2 errors

updated my answer

Script does not work as an initialiser (no errors in log file)

Script works when mapped to the issue type field with some issues.

  • If I change the issue type it would no validate the current selected component.
  •  Components with spaces don't show in the list.

 

Thank you very much @JamieA , it works and was very helpful.

Hint: Cascading field.

Thanks Jamie, too much coding. Hope it helps Aron. Thanks.

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you