Forums

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

How do I read value(s) from Components field?

Szymon Trocha June 13, 2019

Hi,

I'd like to read value(s) from Components field and then embed them in my custom e-mail with scriptrunner.

How do I retrieve the values (this may be null, single or multiple values I guess)?

Thanks

2 answers

1 vote
Santhosh Kumar Arogyaswamy
Contributor
March 18, 2020

I have tried this method and worked for me

def lstCmp = issue.components.name

def lstCmp = issue.components.name
if(lstCmp.contains("xyz")){
do something...
}

  

Kamal Iqlaas Ismail August 18, 2021

Hi Santosh, 

What do you import?

I tried import com.atlassian.jira.issue.getComponents, but got the error

unable to resolve class

Any pointer?

PD Sheehan
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.
August 18, 2021

You can only import valid classes. Use the Atlassian JAVADocs documentation to see what is available: https://docs.atlassian.com/software/jira/docs/api/8.13.6/

But in many cases, the only import you need is the ComponentAccessor (this refers to the jira java component, not the project component)

getComponent() is a method of the com.atlassian.jira.issue.Issue class.

So if you have an issue object of that class (like you would in a workflow context in scriptrunner), then you can access that method either using the java long-form or the groovy way without needing to import anything.

//issue already exists
def componentNames = issue.getComponents()*.name //get the name from each components
//the following line does the exact same thing
def componentNames = issue.components.name //get the name from each components

But if you want to access a list of components for a project/issue, then you need some imports

import com.atlassian.jira.component.ComponentAccessor

//assumes the issue was already declared by scriptrunner
def project = issue.projectObject

def projectComponentManager = ComponentAccessor.projectComponentManager
def allComponents = projectComponentManager.findAllActiveForProject(project.id)

Like Roger Hughes likes this
0 votes
PD Sheehan
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.
June 14, 2019

issue.components  or issue.getComponents() will give you an array of all the components. That array could be empty, with only 1 or many values.

There you can test for null, or size... or collect the list of names:

"$issue.key has the following components: ${issue.components*.name}"
Szymon Trocha June 15, 2019

Hi Peter,

Thank you for reply. So if I'd like to make a script which reads the components and appends it to summary, could it be:

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

def component = issue.getComponentObjects()

issue.setSummary(issue.components*.name + " " + issue.summary)

 

Also how do I make a condition whether a spcific component is in the array? In case I'd like to perform different actions based on components

PD Sheehan
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.
June 17, 2019

It depends on the specifics of what you want to do. There are many ways to branch the logic of a groovy script.

For example:

if (issue.components.any(it.name == "my test value"){
//do something here

} else {
//do something else here

}

 This may get complicated if you have multiple components. What will you do if 2 components that might cause 1 variation of the summary are present at the same time?

Suggest an answer

Log in or Sign up to answer