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

Create a scripted field to count the number of open issues by reporter

I need assistance in creating a scripted field using Scriptrunner to count the number of open issues by reporter of current issue.

Thank you for any help.

2 answers

2 accepted

0 votes
Answer accepted

This script should work for that:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser

def jql= "project = $issue.projectObject.key and reporter = $user.name and resolution is empty"

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def query = jqlQueryParser.parseQuery(jql)

searchService.searchCount(user,query)

Thank you for example, but i have trouble with it. I need jql like this: 

def jql= "project = 123 and reporter = $issue.reporter and resolution is empty"

but when i use this jql i get result "null". I trying some variants: $issue.getReported(), $issue.reporter.getEmailAddress() - still not work. When i remove "reporter = ... " script work fine. When i put "issue.reporter" in the end of script - i get reporter, work fine. I get error when use "reporter = $issue.reporter" in jql.

What am i doing wrong?

Try 

def jql= "project = 123 and reporter = $issue.reporter.name and resolution is empty"

BTW, if you want to include a more complex method with parens like "getEmailAddress()" you have to enclose the expression in curly brace.

What happens is the that $simple.expression or ${complex.getExpression()} ins the Gstring will be extrapolated when it gets coerced to a String when callling the parseQuery

"${issue.getEmailAddress()}"  is equivalent to "$issue.emailAddress"

Like Николай likes this

Thanks for information. Still not working, get error in log: 

2019-09-27 02:00:14,024 ERROR [runner.ScriptFieldPreviewRunner]: *************************************************************************************
2019-09-27 02:00:14,024 ERROR [runner.ScriptFieldPreviewRunner]: Script field preview failed for field that has not yet been created
com.atlassian.jira.jql.parser.JqlParseException: com.atlassian.jira.jql.parser.antlr.RuntimeRecognitionException: MismatchedSetException(64!=null)
at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseClause(DefaultJqlQueryParser.java:110)
at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseQuery(DefaultJqlQueryParser.java:33)
at Script168.run(Script168.groovy:12)
Caused by: com.atlassian.jira.jql.parser.antlr.RuntimeRecognitionException: MismatchedSetException(64!=null)
at com.atlassian.jira.jql.parser.antlr.LexerErrorHelper.handleError(LexerErrorHelper.java:48)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.recover(JqlLexer.java:130)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.recover(JqlLexer.java:136)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.mERROR_RESERVED(JqlLexer.java:1921)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.mTokens(JqlLexer.java:2684)
at org.antlr.runtime.Lexer.nextToken(Lexer.java:85)
at org.antlr.runtime.BufferedTokenStream.fetch(BufferedTokenStream.java:143)
at org.antlr.runtime.BufferedTokenStream.sync(BufferedTokenStream.java:137)
at org.antlr.runtime.CommonTokenStream.skipOffTokenChannels(CommonTokenStream.java:113)
at org.antlr.runtime.CommonTokenStream.LT(CommonTokenStream.java:102)
at org.antlr.runtime.BufferedTokenStream.LA(BufferedTokenStream.java:174)
at com.atlassian.jira.jql.parser.antlr.JqlParser.operand(JqlParser.java:1696)
at com.atlassian.jira.jql.parser.antlr.JqlParser.terminalClause(JqlParser.java:672)
at com.atlassian.jira.jql.parser.antlr.JqlParser.notClause(JqlParser.java:555)
at com.atlassian.jira.jql.parser.antlr.JqlParser.andClause(JqlParser.java:451)
at com.atlassian.jira.jql.parser.antlr.JqlParser.orClause(JqlParser.java:366)
at com.atlassian.jira.jql.parser.antlr.JqlParser.clause(JqlParser.java:328)
at com.atlassian.jira.jql.parser.antlr.JqlParser.query(JqlParser.java:237)
at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseClause(DefaultJqlQueryParser.java:103)
... 2 more
Caused by: MismatchedSetException(64!=null)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.recover(JqlLexer.java:135)
... 18 more

What version of Jira are you using?

Jira 8.0.2

Adaptavist ScriptRunner 5.6.2.1-jira8 

Can you paste your full script?

I have this working on 7.13.2 and 8.3.2

But I had to make a quick change, make sure the "def user=" line is above the "def jql=" line.

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.issue.Issue

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def jql= "project = PROJ and reporter = $issue.reporter.name and resolution is empty"
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def query = jqlQueryParser.parseQuery(jql)

searchService.searchCount(user,query)

Not sure where to go from here... 

I copied your script exactly and all I changed was the project key in the jql... and that works for me.

2019-09-26 17_00_58-Window.png

Like Николай likes this

Thanks for help, i suggest trouble with my setups, will checkin on clear install.

I maked fresh install latest jira service desk, dont changed any settings, installed scriprunner, truing make script field and get same trouble, script wont work for me. Sad.

Could you explain "script won't work for me"?  What are the error messages or results, and what are you doing differently to what Peter-Dave has done?

Fresh install. Jira 8.4.1, Adaptavist ScriptRunner for JIRA v. 5.6.2.1-jira8

 

2019-09-28_18-45-03.png

Log:

2019-09-28 18:44:53,112 ERROR [runner.ScriptFieldPreviewRunner]: *************************************************************************************
2019-09-28 18:44:53,113 ERROR [runner.ScriptFieldPreviewRunner]: Script field preview failed for field that has not yet been created
com.atlassian.jira.jql.parser.JqlParseException: com.atlassian.jira.jql.parser.antlr.RuntimeRecognitionException: MismatchedSetException(64!=null)
at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseClause(DefaultJqlQueryParser.java:110)
at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseQuery(DefaultJqlQueryParser.java:33)
at Script7.run(Script7.groovy:10)
Caused by: com.atlassian.jira.jql.parser.antlr.RuntimeRecognitionException: MismatchedSetException(64!=null)
at com.atlassian.jira.jql.parser.antlr.LexerErrorHelper.handleError(LexerErrorHelper.java:48)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.recover(JqlLexer.java:130)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.recover(JqlLexer.java:136)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.mERROR_RESERVED(JqlLexer.java:1921)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.mTokens(JqlLexer.java:2684)
at org.antlr.runtime.Lexer.nextToken(Lexer.java:85)
at org.antlr.runtime.BufferedTokenStream.fetch(BufferedTokenStream.java:143)
at org.antlr.runtime.BufferedTokenStream.sync(BufferedTokenStream.java:137)
at org.antlr.runtime.CommonTokenStream.skipOffTokenChannels(CommonTokenStream.java:113)
at org.antlr.runtime.CommonTokenStream.LT(CommonTokenStream.java:102)
at org.antlr.runtime.BufferedTokenStream.LA(BufferedTokenStream.java:174)
at com.atlassian.jira.jql.parser.antlr.JqlParser.operand(JqlParser.java:1696)
at com.atlassian.jira.jql.parser.antlr.JqlParser.terminalClause(JqlParser.java:672)
at com.atlassian.jira.jql.parser.antlr.JqlParser.notClause(JqlParser.java:555)
at com.atlassian.jira.jql.parser.antlr.JqlParser.andClause(JqlParser.java:451)
at com.atlassian.jira.jql.parser.antlr.JqlParser.orClause(JqlParser.java:366)
at com.atlassian.jira.jql.parser.antlr.JqlParser.clause(JqlParser.java:328)
at com.atlassian.jira.jql.parser.antlr.JqlParser.query(JqlParser.java:237)
at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseClause(DefaultJqlQueryParser.java:103)
... 2 more
Caused by: MismatchedSetException(64!=null)
at com.atlassian.jira.jql.parser.antlr.JqlLexer.recover(JqlLexer.java:135)
... 18 more

JIRA Service Desk 4.4.1

Project type: customer service

Looks like the query parser doesn't like the JQL that we generate.

Let's try to add some debug message.

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.issue.Issue

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
log.debug "Reporter name is: ${issue.reporter?.name}"
//I think name and username are supposed to be the same, but let's be sure
log.debug "Reporter Username is: ${issue.reporter?.username}"
def jql= "project = TEST and reporter = $issue.reporter.name and resolution is empty"
log.debug "JQL is: $jql"
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def query = jqlQueryParser.parseQuery(jql)

searchService.searchCount(user,query)

Then post the logs (before the error) and also, paste the JQL exactly as it appears int he log into your Jira Issue Navigator and show us what it renders.

Thanks for idea with logs! Working jql for me is:

def jql = "project = TEST and reporter = '$issue.reporter.name' and resolution is empty"

Working scripted field for me (thanks for @Peter-Dave Sheehan):

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.issue.Issue

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def jql = "project = $issue.projectObject.key and reporter = '$issue.reporter.name' and statusCategory != Done"
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def query = jqlQueryParser.parseQuery(jql)

searchService.searchCount(user,query)

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Comparison between Zephyr & Xray

I am sharing this article with both Zephyr and Xray information and everything should be in one place. It will be easy to know when selecting Zephyr or Xray. Zephyr and Xray are both Jira plug-ins u...

180 views 1 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