Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

0 votes
Answer accepted

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

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

How we use Jira Service Management for our recruitment process! - Part 2

It is never about setting up a process and being done with it. Rather, the focus should always be on optimizing it for the best outcomes. Thus, we didn’t stop at setting up JSM for our recruitment pr...

188 views 0 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you