How to edit a Kanban Board quick filter using groovy?
Asked by Stefan Stadler:

Hi,

I am trying to edit several hundreds of Kanban Board quick filters to reflect changed values.

I am using the below code and get until I can actually update the filter. However, the update is failing with a NullPointerException for some reason. I have already checked all of the passed in parameters to this function and they all have values unequal to null.

I am using ScriptRunner console to run this. Imports have been done correctly

Can you please help?

Thanks!

```
import com.atlassian.greenhopper.model.rapid.QuickFilter
import com.atlassian.greenhopper.service.rapid.view.RapidViewService
import com.atlassian.greenhopper.service.rapid.view.QuickFilterService
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.JiraAgileBean
import com.atlassian.greenhopper.model.rapid.QuickFilter.QuickFilterBuilder
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.greenhopper.model.validation.*
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()

if(rapidViewService) {
    def rapidViews = rapidViewService.getRapidViews(applicationUserInput).value

    rapidViews.each {

        List<QuickFilter> rapidViewsIn = quickFilterService.loadQuickFilters(it).each{qf ->

            //Just some arbitrary change to change a default filter
            String newQuery = qf.query.replaceAll("-1d", "+2d").toString()
            def qfBuilder = new QuickFilterBuilder()
            QuickFilter newQf = qfBuilder.query(newQuery).description(qf.description).name(qf.name).id(qf.id).position(qf.position).build()

            def update = quickFilterServiceImpl.update(applicationUserInput, it, newQf, errorCollection)
        }
    }
}
```


Answers:
author: Andre Krueger
body: Hi [@Stefan Stadler](/t5/user/viewprofilepage/user-id/4469283)

1) the issue is, that you're triggering the update using

```
quickFilterServiceImpl.update(...)
```

which has been created by calling the default constructor

instead of using

```
quickFilterService.update(...)
```

which is provided by the container.

Without knowing the details of the implementing class, I assume that some service that is supposed to be injected into QuickFilterService remains null. Once you change it, the NPE should disappear.

2) I would recommend passing the QuickFilter object to the builder, so that you do not need to copy field by field individually, but only the adjusted query.

```
def newQf = new QuickFilterBuilder(qf)
    .query(newQuery)
    .build()
```

Kind regards,  
Andre

comments: author: Stefan Stadler
body: Hi [@Andre Krueger](/t5/user/viewprofilepage/user-id/4661924)

thanks for the update.

You are right, I just had to use the QuickFilterService from the JiraAgileBean.

Now it is working using the following code:  

```
import com.atlassian.greenhopper.model.rapid.QuickFilter
import com.atlassian.greenhopper.service.rapid.view.RapidViewService

import com.atlassian.jira.component.ComponentAccessor

import com.onresolve.scriptrunner.runner.customisers.JiraAgileBean
import com.atlassian.greenhopper.model.rapid.QuickFilter.QuickFilterBuilder
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.greenhopper.model.validation.ErrorCollection
import com.atlassian.greenhopper.service.rapid.view.QuickFilterService

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

@JiraAgileBean
RapidViewService rapidViewService

@JiraAgileBean
QuickFilterService quickFilterService

// Change this constant to the string you want to search for
String oldText = 'assignee = EMPTY'
String newText = oldText + " AND reporter != currentUser()"
String kanbanName = "Your Board Name here"

def applicationUserInput = ComponentAccessor.jiraAuthenticationContext.loggedInUser

if(rapidViewService) {
    def rapidViews = rapidViewService.getRapidViews(applicationUserInput).value
    rapidViews.each{
        if(it.name==kanbanName){
            List<QuickFilter> rapidViewsIn = quickFilterService.loadQuickFilters(it).each{qf ->
                if(qf.query.contains(oldText)){
                    String newQuery = qf.query.replaceAll(oldText, newText).toString()
                    def qfBuilder = new QuickFilterBuilder(qf)
                    QuickFilter newQf = qfBuilder.query(newQuery).build()

                    quickFilterService.update(applicationUserInput, it, newQf, new ErrorCollection())
                }
            }
        }
    }
}
```

Again, thanks a lot for your help on that!


author: Stefan Stadler
body: I have also tried it using the set method instead of the update method, but this also fails with a NullPointerException. At seems that the error is not related to the ErrorCollection but instead to some other argument. However, as I mentioned, I have also included some logging on my side and can confirm that non of these objects are actually null.  

```
...
rapidViews.each {
      List<QuickFilter> newFilters = []

      List<QuickFilter> rapidViewsIn = quickFilterService.loadQuickFilters(it).each{qf ->

            String newQuery = qf.query.replaceAll("-1d", "+2d").toString()

            def qfBuilder = new QuickFilterBuilder()

            QuickFilter newQf = qfBuilder.query(newQuery).description(qf.description).name(qf.name).id(qf.id).position(qf.position).build()

            newFilters.add(newQf)

       }

       def update = quickFilterServiceImpl.set(applicationUserInput, it, newFilters) //<-- this is where it fails

}
```


Tags: groovy

jira-server

server