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

Update a board's Quick Filter in Jira using Groovy (ScriptRunner) - Jira Data Center

Rinaldi James Michael
Contributor
March 25, 2023 edited

I have tried using the below code and got a NullPointerException error log. I've imported as many classes as I could find. Some even twice XD

Maybe I'm using the update function wrong or the ErrorCollection wrong. Any suggestions/solutions are greatly appreciated.

 

 

 

 

 

import com.onresolve.scriptrunner.runner.customisers.JiraAgileBean
import com.atlassian.jira.user.ApplicationUser
import com.onresolve.scriptrunner.parameters.annotation.*
import com.atlassian.greenhopper.service.rapid.view.RapidViewService
import com.atlassian.greenhopper.service.rapid.view.QuickFilterService
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.greenhopper.model.rapid.QuickFilter.QuickFilterBuilder
import com.atlassian.greenhopper.service.rapid.view.QuickFilterService.*
import com.atlassian.greenhopper.service.rapid.view.QuickFilterServiceImpl.*
import java.lang.String.*

import java.io.*
//import com.atlassian.jira.util.ErrorCollection
import com.onresolve.scriptrunner.runner.customisers.JiraAgileBean
import com.atlassian.jira.user.ApplicationUser
import com.onresolve.scriptrunner.parameters.annotation.*
import com.atlassian.greenhopper.*
import com.atlassian.greenhopper.service.rapid.view.RapidViewService
import com.atlassian.greenhopper.model.rapid.QuickFilter
import com.atlassian.greenhopper.service.rapid.view.*
import com.atlassian.greenhopper.service.rapid.view.QuickFilterService.*
import com.atlassian.greenhopper.service.rapid.view.QuickFilterServiceImpl.*
import com.atlassian.greenhopper.model.rapid.QuickFilter.QuickFilterBuilder
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.greenhopper.service.rapid.view.QuickFilterServiceImpl.update.*
import com.atlassian.greenhopper.model.validation.*
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.greenhopper.web.rapid.view.RapidViewHelper
import com.atlassian.greenhopper.service.rapid.view.QuickFilterServiceImpl


@WithPlugin("com.pyxis.greenhopper.jira")

@JiraAgileBean
RapidViewService rapidViewService


@JiraAgileBean
QuickFilterService quickFilterService

@JiraAgileBean
RapidViewHelper rapidViewHelper

def authenticationContext = ComponentAccessor.jiraAuthenticationContext
def applicationUserInput = authenticationContext.loggedInUser
QuickFilterServiceImpl quickFilterServiceImpl = new QuickFilterServiceImpl()
def errorCollection = new ErrorCollection()

def rapidViews = rapidViewService.getRapidView(applicationUserInput,1234).get()
def rapidViewsIn = quickFilterService.loadQuickFilters(rapidViews)
def quickFiltersIn = rapidViewsIn.query

def newQuickFilter = quickFiltersIn[3].replaceAll("To replace this JQL","This is now replaced").toString()

def newQuickFilterBuilder = new QuickFilterBuilder()
newQuickFilterBuilder.query(newQuickFilter)

def newQuickFilterBuild = newQuickFilterBuilder.build()



def update = quickFilterServiceImpl.update(applicationUserInput,rapidViews,newQuickFilterBuild,null)

2 answers

0 votes
Andre Krueger August 30, 2024 edited

Hi @Rinaldi James Michael

please check How to edit a Kanban Board quick filter using groovy? which is proving the solution for your problem.

Kind regards
Andre

0 votes
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 28, 2023

Ah, a challenge :-)

Imports are not a problem - you'd get "can't find out how to do this" type errors.  Like "please multiply 6 by blue", the message would be "blue is not something any of our libraries know how to multiply by 6".  If your imports were wrong, you'd get "unrecognisable function" type messages.

NPEs are what Java throws when you try to say, "do this with a null/incomprehensible value", and the function can't cope.  (Don't blame the developers, they can't code for every possible NPE all the time, albeit I'm really bad at it)

So.  Your error report should be reporting the (first) line an NPE occurs.  One of the (not large numbers of) things I like about Java is that it reports the line of code it failed on, unlike the languages I learned to code in before 2004.

If you've got a 250-line script and you load it up in an editor that shows you line numbers, when you get an NPE reporting line 42, it really is happening on line 42, including all the empty lines you used to make it more readable.

So.  Can you tell us what line the NPE happens on?  Given your script posted above, what I would need to get started is a minimal "it happens on the line that says "XYZ"

Rinaldi James Michael
Contributor
March 28, 2023

The line that throws the NullPointerException (NPE) is:

def update = quickFilterServiceImpl.update(applicationUserInput,rapidViews,newQuickFilterBuild,null)
The log is: 
java.lang.NullPointerException at com.atlassian.greenhopper.service.rapid.view.QuickFilterServiceImpl.update(QuickFilterServiceImpl.java:145) at com.atlassian.greenhopper.service.rapid.view.QuickFilterService$update$0.call(Unknown Source) at Script490.run(Script490.groovy:68)
groovy:68 is line 65 of the code I shared in the description. 
Like • Nic Brough -Adaptavist- likes this
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 29, 2023

Thanks!

So this is where you need to start working backwards - that update function needs broadly what you've named the variables you've put into it - the user to act as, the rapidview (board), a quickfilter, and an error collection object.

The user looks fine to me, so I'd start with the other three. 

The easiest one to amend would be the error collection - you've already created the object, but you've given the function null, so change it to the error collection instead.

If that doesn't work, or even just change the error message, then I'd look at the other two - one of them probably does not contain what the function needs.  

You could try to trace it back through the code, but you could do a much quicker and brutal test - try dumping the content of them in a simple log string "log.debug (rapidViews + "/" + newQuickFilterBuild)" 

Java will try to convert it to something moderately human-readable, but the obvious one is if you get a null in one, or both, you need to look at the code that is populating the variable.

Like • Rinaldi James Michael likes this
Rinaldi James Michael
Contributor
April 10, 2023

I've narrowed down the source of the error. It's the errorCollection parameter. The only issue now is I do not know how to populate that. 

The errorCollection in use in the update function is from the Greenhopper library: https://docs.atlassian.com/jira-software/6.3.2.1/com/atlassian/greenhopper/model/validation/ErrorCollection.html

Unlike the errorCollection in https://docs.atlassian.com/software/jira/docs/api/7.4.1/com/atlassian/jira/util/ErrorCollection.html. The Greenhopper version has no known implementing classes. 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
atlassian, ace, atlassian community event, donation, girls who code, women in tech, malala fund, plan international, kudos, community badge, badge, atlassian badge, International Women’s month, International Women’s Day, women's month, women's day

10 for Change at Atlassian Community Events

Show up and give back by attending an Atlassian Community Event: we’ll donate $10 for every event attendee in March!

Join an Atlassian Community Event!
AUG Leaders

Upcoming Jira Events