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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

Validate JQL result on issue creation

This is my scenario:

I need to do a JQL and get the number of results (done), if this number is grater than X number do not allow the creation of an issue.

 

This is my current (example) code:

 

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.web.bean.PagerFilter

final String jqlSearch = 'project = "Sample Project"' // the jql query you want to search with

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchService = ComponentAccessor.getComponentOfType(SearchService)
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
def total_issues = 0
if (parseResult.isValid()) {
try {
def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter)
def issues = results.issues

//log.warn(results)
issues.each {
//log.warn(it.key)
total_issues++
}
return total_issues
} catch (SearchException e) {
e.printStackTrace()
}
} else {
print("Invalid query")
return null
}

if (total_issues >= 23){
return false
}

Right now is not working, I put this code on the 'Create' transition. 

1 answer

1 accepted

1 vote
Answer accepted

Hi daniel,

Why dont use SearchService.searchCountOverrideSecurity(ApplicationUser searcher, Query query) method?

Which indeed returns the number of issues that matches the given query.


You code should looks like something like this (I've just type the code here so probably I missed something)


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.opensymphony.workflow.InvalidInputException


def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()

// edit this query to suit
def query = jqlQueryParser.parseQuery("project = JRA and assignee = currentUser()")

def count = searchService.searchCountOverrideSecurity(user, query)


if(count >= 23 ) {
    throw new InvalidInputException("Too Many Issues")
}


Regards

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Confluence

What do you think is the most *delightful* Confluence feature? Comment for a prize!

- Create your own custom emoji 🔥 - "Shake for Feedback" on mobile 📱 - An endless supply of GIFs via GIPHY 🤩 Is there anything quite as nice as a pleasant surprise? Comment below with what...

342 views 23 8
Join discussion

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