Getting issue component in postfunction

I'm writing a postfunction in the "create issue" workflow step with the ScriptRunner plugin

The script should check if the issue has a specific component named "myComponent" and returns true in order to fire an event

 

I tried this:

issue.component = "myComponent"

Too simple apparently. So I looked in the documentation and tried this in the Script Console:

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssue("JIRA-001")

For each issue key I try with the component appears to be `null`

[timespent:null, timeoriginalestimate:null, project:10022, description:null, type:6, resolution:null, number:24404, security:null, resolutiondate:null, fixfor:null, id:67268, key:null, summary:2nd test wit component, watches:1, creator:test-charlie, created:2015-09-18 09:50:15.0, reporter:test-charlie, priority:3, environment:null, component:null, timeestimate:null, duedate:null, votes:0, assignee:otheruser, updated:2015-09-18 09:50:15.0, workflowId:88798, status:1]

 

What am I missing?

I also tried getComponentObjects()

 

2 answers

1 accepted

1 vote
"myComponent" in issue.componentObjects*.name
1 vote

Components is a multiple option field, which can have none, one, or many settings. 

You're finding "component: null" as a hangover from code from when the component was a single select, so you should ignore that.

Use issue.getComponentObjects() to get a collection of values. 

This is where my java/groovy lets me down - I don't know if there's an easy way to say "is the string 'My Component' represented by a projectComponent in this collection?" directly. 

What will work is brute force - iterate over the result and look at the names -

collectionOfComponents.each { if it.getName().equals('My Component') then {...} }

Will do it if you want to compare on component name.

the spread-dot operator is your friend here, componentObjects*.name returns a new List by calling getName on each object.

Thanks for telling me what that 'component' attribute was ;) I tried both approaches and they work. The notation from Jamie seems the way to go Also: I was using get getIssue() instead of getIssueObject() in the script console which is deprecated

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

160 views 0 4
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you