Display the results of a JQL search in a custom field

Hi, I am trying to configure a behaviour that will display the results of a JQL search in a Custom field of similar issue.

The basic idea is I need to display the field values previous issues similar to the current issue (where the custom field is available)

1 answer

0 votes

Hi Praveen,

I would probably not use a behaviour for this. The reason being is that every time you do anything to the issue (Create, Transition, Edit, etc) the behaviour will run the JQL function repeatedly. You could try putting the code in an initializer function behaviour, but there is currently an open bug about not being able to set formField values in intializers, so I don't know if that would work.

Instead, I'd set up a Script Listener that is applied to the specific project you want to set values for. You can set the Script Listener to fire on "Issue Created" so that the field gets set whenever a new issue in that project is created.

Below is a script that runs a specific JQL query. Once it gets the results from that query, it loops through the issues from the result. In my script, I match an issue that has a key of "JRA-2". If I find that specific key, I get the custom field value from that issue. After I get the custom field value from that issue, I update the custom field value for the current issue. Below is the script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// The search query
def query = jqlQueryParser.parseQuery("project = JRA")

// Results from query
def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())

// Get the custom field that you want to update
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectByName("TextFieldA")
def valueFromPreviousIssue

// Loop through the search results. If the key of one of those issues matches, grab the value from its custom field
results.getIssues().each { documentIssue ->
def oldIssue = issueManager.getIssueObject(documentIssue.id)
if (oldIssue.key == "JRA-2") {
def oldCustomField = customFieldManager.getCustomFieldObjectByName("TextFieldA")
valueFromPreviousIssue = oldIssue.getCustomFieldValue(oldCustomField)
}
}
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), valueFromPreviousIssue), new DefaultIssueChangeHolder())

Note that you will obviously need to change the JQL query to match what you want to search for. You'll also need to change the part in the loop, since you don't want to match for "JRA-2".

You might get static type checking errors when you use this code, but you can ignore this. I've tested the script and can see that it works.

Suggest an answer

Log in or Sign up to answer
Community showcase
Asked Thursday in Jira Ops

I'm John Allspaw, Ask Me Anything about incident analysis and postmortems

I'm John Allspaw, co-founder of   Adaptive Capacity Labs, where we help teams use their incidents to learn and improve. We bring research-driven methods and approaches to drive effective inciden...

452 views 2 5
View question

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