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
Community Members
Community Events
Community Groups

Need help to Set custom field for issues with the same labels

  1. Hello

My team did some import for X-RAY and we need to associate tests with preconditions based on the labels. 

The only common field is the labels. 

The prob is that the precondition field is a custom one that need issue key. we have to specify the issue key to do the association.

I'm not that good in scriptrunner nor groovy.

If someone could help us to script this.

Big thanks

1 answer

0 votes
Antoine Berry Community Leader Dec 06, 2021

Hi @Oussama Brik ,

I am not sure to understand fully your problem, but here is a groovy script that will return issues with the label "test" : 

import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.web.bean.PagerFilter

def jqlSearch = "labels = \"test\""

SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
def userManager = ComponentAccessor.getUserManager()
IssueManager issueManager = ComponentAccessor.getIssueManager()
ApplicationUser user = userManager.getUserByKey('berry_a')
List<Issue> issues = null
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
def searchResult =, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.results.collect {issueManager.getIssueObject(}
} else {
log.error("Invalid JQL: " + jqlSearch);
return issues

Hope that helps

thanks @Antoine Berry  I found a way to solve my prob:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Level
import org.apache.log4j.Logger
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)
def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def GetIssueManager = ComponentAccessor.getIssueManager()
def log = Logger.getLogger("com.acme.workflows")

final customFieldName = "customfield_12345"
def IssueCF = customFieldManager.getCustomFieldObject(customFieldName)

def query1 = jqlQueryParser.parseQuery('project = MyProject AND (type = bug) AND (labels is not Empty) ')
def results1 =, user, PagerFilter.getUnlimitedFilter())
def List1 = results1.issues.collect{issueManager.getIssueByCurrentKey(it.key)}

def query2 = jqlQueryParser.parseQuery('project = MyProject AND (type = task) AND (labels is not Empty)')
def Results2 =, user, PagerFilter.getUnlimitedFilter())
def List2 = Results2.issues.collect{issueManager.getIssueByCurrentKey(it.key)}

Results2.getIssues().each { documentIssue ->
def Issue2Key = documentIssue.key
def Label2 = documentIssue.labels
String Label2_String = Label2.first()

results1.getIssues().each { documentIssue2 ->
def Issue1Key = documentIssue2.key
def Issue1Object = issueManager.getIssueObject(Issue1Key)
def Issue1Label = documentIssue2.labels
String Label1_String = Issue1Label.first()
def Issue1CF = documentIssue2.getCustomFieldValue(IssueCF)
if(Label1_String.equals(Label2_String) && !Issue1CF){
Issue1Object.setCustomFieldValue(IssueCF, [Issue2Key])
GetIssueManager.updateIssue(user, Issue1Object, EventDispatchOption.DO_NOT_DISPATCH, false)

Like Antoine Berry likes this

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Jira Service Management

Improving the Create Issue Experience in Jira Service Management Cloud

Hello everyone!  We are very excited to announce some much needed changes to the issue create experience in JSM (the blue "create" button) at the top of the screen.  We have just starte...

208 views 7 7
Read article

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