Need groovy script to update the filters
Asked by Meghana K V:

Need groovy script to run from Jira script runner to update the filters:

Fetch all the JQL filters based on issue type which is equal to "xyz",

then exclude the ones with issue type "Profile xyz", then update the filters with issue type as "feature".


Answers:
author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Meghana K V](/t5/user/viewprofilepage/user-id/5327442)

Could you provide a little bit more information, i.e. what Automation function are you using for this? Is the Issue Create or Transition Issue?

It would be beneficial if you could share a screenshot of your Automation configuration.

I am requesting this so I can provide a sample script.

Thank you and Kind regards,

Ram

comments: author: Meghana K V
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) ,

Thank you for your reply.

I have created the following script for the requirement mentioned above, but it's not working, please let me know what I am missing.

I have to fetch all the filters with issuetype = EPIC and exclude the ones with issuetype = Portfolio EPIC and then to the resulting filters, I need to change the issuetype to Feature.  

```
import com.atlassian.jira.user.ApplicationUser

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.bc.filter.SearchRequestService

import com.atlassian.jira.issue.search.SearchRequest

import com.atlassian.jira.bc.user.search.UserSearchParams

import com.atlassian.jira.bc.user.search.UserSearchService

import com.atlassian.jira.bc.issue.search.SearchService

import com.atlassian.jira.bc.JiraServiceContextImpl

import java.lang.StringBuffer

 

SearchRequestService searchRequestService = ComponentAccessor.getComponent(SearchRequestService.class)

UserSearchService userSearchService = ComponentAccessor.getComponent(UserSearchService)

SearchService searchService = ComponentAccessor.getComponent(SearchService)

def sb = new StringBuffer()

def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def ctx = new JiraServiceContextImpl(loggedInUser)

 

UserSearchParams userSearchParams = new UserSearchParams.Builder()

    .allowEmptyQuery(true)

    .includeInactive(false)

    .ignorePermissionCheck(true)

    .maxResults(5000)

    .build()

 

// Define the issue type name to search for

def issueTypeName = "EPIC"

def excludedIssueTypeName = "Portfolio EPIC" // Add the excluded issue type name here

def newissueTypeName = "Feature"

 println "Starting script execution"

// Iterate over each user's filters

userSearchService.findUsers("", userSearchParams).each { ApplicationUser filter_owner ->

    try {

        searchRequestService.getOwnedFilters(filter_owner).each { SearchRequest ->

            def query = SearchRequest.query

            

            def queryString = searchService.getJqlString(query)

            println("Old Filter '$queryString' updated successfully.")

            

            if (queryString.contains(issueTypeName) && !queryString.contains(excludedIssueTypeName)) {

            

            String newQueryString = queryString.replace(issueTypeName, newissueTypeName)

            

            def newQuery = searchService.parseQuery(filter_owner, newQueryString).query

            sb.append("Filter for ${filter_owner.displayName}: ${query.name}, ${query.getPermissions().isPrivate() ? 'Private' : 'Shared'}\n")

            SearchRequest.setQuery(newQuery)

            

            searchRequestService.updateFilter(ctx, SearchRequest)

        }

    } 

    }catch (Exception e) {

        // If filter is private or encounters an error

        sb.append("Unable to get filters for ${filter_owner.displayName} due to ${e}\n")

    }

}

 

// Output results

return sb.toString()
```


Tags: scriptrunner