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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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)

3 answers

Hi @Joshua Yamdogo _ Adaptavist  and @kumar jira

Could you please let me know how to fetch the value of a custom field and summary of issues fetched from JQL query and then concatenate the two to make an option of a select list custom field. Here's the detailed explanation:

https://community.atlassian.com/t5/Jira-questions/Is-it-possible-to-have-a-scripted-select-list-which-contain/qaq-p/1211810#U1216440

It is very urgent and important for me. Any help would be appreciated.

Thanks and Regards,

Swapnil Srivastav

@Joshua Yamdogo _ Adaptavist 

Am working on something similar.

Hi,

Using behaviors i would like to get list of a field values from  issues result of a JQL.

[For example, the field name is "Field 123", 

I want to pass a JQL from behaviors and get the list of "Filed 123" values from all the issues.]

----

This is what i tried.

def query = jqlQueryParser.parseQuery("issuetype = 'Some issue type' and status != 'Not Implement'")
def searchRequest = new SearchRequest(query)
def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())
results.getIssues().each { documentIssue ->
def issue = issueManager.getIssueObject(documentIssue.id)
def release = customFieldManager.getCustomFieldObjectByName("Release")
log.warn("789")
def releaseval = issue.getCustomFieldValue(release)
def optionsMap2 = [:]

Thank you. 

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
TAGS
Community showcase
Published in Jira

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across Jira Cloud instance.   📢 Get y...

117 views 6 5
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