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

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

Oussama Brik November 23, 2021
  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

Suggest an answer

Log in or Sign up to answer
0 votes
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 6, 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

Oussama Brik January 21, 2022

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
AUG Leaders

Atlassian Community Events