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

Remove some options(not all) from a select list using groovy

Gaurav Arora _Wipro_
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.
November 8, 2019

Hi All,

I need to delete some values from a Select List Custom Filed.

I have checked other pages in Community where I found how to delete all the options but that is not my requirement.

I only want to remove some selected options so that all other option Values are untouched and their option Ids are intact.

 

Kindly help me out here.

Thanks in advance.

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Peter Weimer
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 27, 2021

Had the same problem ("delete all unused option from select list") ..   
after long research I found a solution that works for me:
* loop over all options ( for current Fld-Configuration )
* define JQL-Query -> if option is NOT used ( = JQL.Query-Matches = 0 )
* then remove option ..
* "user" should be admin -> but i'm not sure if necessary ..

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.config.FieldConfig;

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.customfields.option.Option

import com.atlassian.jira.issue.search.*
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.context.IssueContextImpl;

def getJQLcnt ( String queryString, ApplicationUser user ) { // Returns number of filter-matches
JqlQueryParser jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
SearchProvider searchProvider = ComponentAccessor.getComponent(SearchProvider)
def query = jqlQueryParser.parseQuery(queryString)
SearchResults results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())
results.total
}

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getUser() // needed for JQL-Query
CustomField cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("BUB Kostenstelle"); // Place your selcet-List custom fieldname
def cft = cf.getCustomFieldType()
Issue issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("BBB-540"); // place an issue key that uses that custom field -> to get Context + FldConfiguration
IssueContextImpl issueContext = new com.atlassian.jira.issue.context.IssueContextImpl(issue.getProjectId(), issue.getIssueTypeId());
FieldConfig fieldConfig = cf.getRelevantConfig(issueContext);
OptionsManager optionsManager = ComponentAccessor.getOptionsManager();
List<Option> options = optionsManager.getOptions(fieldConfig);

// String s = ""
for (Option option in options) { // loop on all options
String optVal = option.getValue()
String jqlStr = "'BUB Kostenstelle' = " + optVal // define JQL-Query for Condition
if ( getJQLcnt(jqlStr,user) == 0) // if cond > 0 --> remove option from current CF-Configuration
{ // s = s + " " + optVal
optionsManager.deleteOptionAndChildren(option);
}
}
// s

Gaurav Arora _Wipro_
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.
February 27, 2021

Hi @Peter Weimer

It's been so long when I wanted to use this. But still thanks a lot! It might come in handy in the future.

 

Br, 

Gaurav

TAGS
AUG Leaders

Atlassian Community Events