Scripted Field to return portfolioChildrenOf
Asked by Jamie Rogers:

Greetings

I am needing assistance creating a scripted field to return the results of children issues from our roadmap plans, basically the inverse of "Parent Link" field. We can query this via JQL and it returns the correct results for a given issue key using the portfolioChildrenOf issueFunction (ScriptRunner JQL function).

Example JQL: issueFunction in portfolioChildrenOf("issue in (AJJ-322)")

This returns children issues (epics in this case) of the issue key which is a feature, above epic in our plan hierarchy.

Below is the script code I have tried to implement but it is not returning any results ; its throwing what appears to be a JQL parsing error (also listed below). I am hoping someone can assist or advise what might be happening in my script or aid with a solution. Thanks.

*** ** * ** ***

**SCRIPTED FIELD CODE:**

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

def issueManager = ComponentAccessor.getIssueManager()  
def searchService = ComponentAccessor.getComponent(SearchService.class)  
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()  
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)

def issue = issueManager.getIssueObject(issue.key)  
def jqlQuery = jqlQueryParser.parseQuery("issueFunction in portfolioChildrenOf('" + issue.key + "')")

def searchResults = searchService.search(user, jqlQuery, PagerFilter.getUnlimitedFilter())

return searchResults.getResults().size()  

*** ** * ** ***

**ERROR FROM LOGS:**  
*2024-03-22T15:55:07,087 ERROR \[jql.ScriptedFunctionClauseFactory\]: Error in ScriptRunner JQL function clause {issueFunction in portfolioChildrenOf(AJJ-322)} com.atlassian.jira.jql.parser.JqlParseException: com.atlassian.jira.jql.parser.antlr.RuntimeRecognitionException: NoViableAltException(-1@\[\]) at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseClause(DefaultJqlQueryParser.java:110) \~\[classes/:?\] at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseQuery(DefaultJqlQueryParser.java:33) \~\[classes/:?\] at com.atlassian.jira.jql.parser.JqlQueryParser$parseQuery$14.call(Unknown Source) \~\[?:?\] at com.onresolve.jira.groovy.jql.AbstractScriptedJqlFunction.search(AbstractScriptedJqlFunction.groovy:211) \~\[plugin.4110092473777456710.groovyrunner-8.22.0.jar:8.22.0\] at com.onresolve.jira.groovy.jql.AbstractScriptedJqlFunction$search$0.callCurrent(Unknown Source) \~\[?:?\] at com.onresolve.jira.groovy.jql.relations.portfolio.PortfolioChildrenOf.getQuery(PortfolioChildrenOf.groovy:28) \~\[plugin.4110092473777456710.groovyrunner-8.22.0.jar:8.22.0\] at com.onresolve.jira.groovy.jql.JqlQueryFunction$getQuery.call(Unknown Source) \~\[?:?\] at com.onresolve.jira.groovy.jql.ScriptedJqlFunction.getQuery(ScriptedJqlFunction.groovy:135) \~\[plugin.4110092473777456710.groovyrunner-8.22.0.jar:8.22.0\] at com.onresolve.jira.groovy.jql.ScriptedJqlFunction$getQuery.call(Unknown Source) \~\[?:?\] at com.onresolve.jira.groovy.jql.ScriptedFunctionClauseFactory.getQuery(ScriptedFunctionClauseFactory.groovy:110) \~\[plugin.4110092473777456710.groovyrunner-8.22.0.jar:8.22.0\] Caused by: com.atlassian.jira.jql.parser.antlr.RuntimeRecognitionException: NoViableAltException(-1@\[\]) at com.atlassian.jira.jql.parser.antlr.JqlParser.reportError(JqlParser.java:203) \~\[classes/:?\] at com.atlassian.jira.jql.parser.antlr.JqlParser.operator(JqlParser.java:1362) \~\[classes/:?\] at com.atlassian.jira.jql.parser.antlr.JqlParser.terminalClause(JqlParser.java:645) \~\[classes/:?\] at com.atlassian.jira.jql.parser.antlr.JqlParser.notClause(JqlParser.java:555) \~\[classes/:?\] at com.atlassian.jira.jql.parser.antlr.JqlParser.andClause(JqlParser.java:432) \~\[classes/:?\] at com.atlassian.jira.jql.parser.antlr.JqlParser.orClause(JqlParser.java:366) \~\[classes/:?\] at com.atlassian.jira.jql.parser.antlr.JqlParser.clause(JqlParser.java:328) \~\[classes/:?\] at com.atlassian.jira.jql.parser.antlr.JqlParser.query(JqlParser.java:237) \~\[classes/:?\] at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseClause(DefaultJqlQueryParser.java:103) \~\[classes/:?\] ... 9 more Caused by: org.antlr.runtime.NoViableAltException at com.atlassian.jira.jql.parser.antlr.JqlParser.operator(JqlParser.java:1192) \~\[classes/:?\] at com.atlassian.jira.jql.parser.antlr.JqlParser.terminalClause(JqlParser.java:645) \~\[classes/:?\] at com.atlassian.jira.jql.parser.antlr.JqlParser.notClause(JqlParser.java:555) \~\[classes/:?\] at com.atlassian.jira.jql.parser.antlr.JqlParser.andClause(JqlParser.java:432) \~\[classes/:?\] at com.atlassian.jira.jql.parser.antlr.JqlParser.orClause(JqlParser.java:366) \~\[classes/:?\] at com.atlassian.jira.jql.parser.antlr.JqlParser.clause(JqlParser.java:328) \~\[classes/:?\] at com.atlassian.jira.jql.parser.antlr.JqlParser.query(JqlParser.java:237) \~\[classes/:?\] at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseClause(DefaultJqlQueryParser.java:103) \~\[classes/:?\] ... 9 more*


Answers:
author: Fabio Racobaldo _Herzum_
body: Hi [@Jamie Rogers](/t5/user/viewprofilepage/user-id/1033381) ,

portfolioChildrenOf() function requires a subquery as parameter (<https://docs.adaptavist.com/sr4js/latest/features/jql-functions/included-jql-functions/portfolio>)

Example :

    issueFunction in portfolioChildrenOf("status = 'To Do'")

Try to update your code from :

def jqlQuery = jqlQueryParser.parseQuery("issueFunction in portfolioChildrenOf('" + issue.key + "')")

to

def jqlQuery = jqlQueryParser.parseQuery("issueFunction in portfolioChildrenOf(\\"issuekey in('" + issue.key + "')\\")")

Please let me know if it works,

Fabio

comments: author: Jamie Rogers
body: Thanks this fixed the issue.

I did have 'issue in (issue_key)' in the JQL (which makes sense why it worked there) but didn't realise this was missing in the code ;(

Always good to get a second pair of eyes across these things, so much appreciated!


author: Fabio Racobaldo _Herzum_
body: you're welcome [@Jamie Rogers](/t5/user/viewprofilepage/user-id/1033381)


Tags: data-center

jira-software-data-center

portfolio-for-jira-data-center

portfolio-for-jira-server

scriptrunner

server