<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=949806081816595&amp;ev=PageView&amp;noscript=1">
Go Back

A Simple Groovy Script for Bulk Disabling Custom Field Values in Jira

Amanda Kirk Amanda Kirk | September 17, 2020 | 3 MIN READ
script, groovy, custom fields, jira, atlassian

Untitled-21

Here at Isos Technology, we've seen use cases for pretty much everything. One use case that has come up repeatedly, for both my clients and myself as an admin, has been bulk disabling custom field values. Say you have a select list field with dozens of values and half of them have gone out of commission, or you have a field that had 200 values but you want to replace it with four. This obviously would be painful to do manually one by one.  So, in this blog post, I will show you how to quickly bulk disable custom field values using a simple Groovy script! To run it, just pull up the Groovy console in Jira.

Let's jump right in: 

 

BulkDisableCFValues.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import com.atlassian.jira.component.ComponentAccessor
def sb = new StringBuffer()
 
//use any issue that uses the proper context/configuration for the CF you would like to update
def issueKey = "CS-2134"
def issue = ComponentAccessor.getIssueManager().getIssueObject(issueKey)
 
 
//define the values you want to disable here
def optionsToDisable = ["Hello", "Goodbye"]
 
//to get the custom field, pass the custom field id
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(17101)
 
//get the custom field options
def optionsManager = ComponentAccessor.getOptionsManager()
def options = optionsManager.getOptions(customField.getRelevantConfig(issue))
 
//disable options
options.each {
  if(optionsToDisable.contains(it.toString())){
        sb.append("Disabling Option: " + it.toString() + "\n")
        optionsManager.disableOption(it)
  }
}
   
return sb.toString()

 

As you can see from lines 4-6, we first reference an issue that uses the proper context for the custom field we want to update. We do this since select list custom fields can have various contexts with different sets of options/values. Once we get that proper context, we can proceed to define the values we want to disable, which we do in line 10. Next, we get the custom field object and the custom field options/values so that we can alter the custom field configuration (its options). Finally, in lines 19-26 we go through the options of the custom field and disable the ones we want to disable (as defined in our array in line 10). 

 

Note that in the above script you will need to replace three values:

  1. The optionsToDisable array - define which values you want to be disabled (line 10)
  2. The custom field id for the field you are wanting to update (line 8)
  3. An issue that uses the proper context for the custom field we want to update (line 5)

 

Now, for the use case of disabling ALL options for a custom field, all you need to do is modify the above script slightly to remove the 'if statement' block, which would look like the below script:

 

BulkDisableCFValues.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import com.atlassian.jira.component.ComponentAccessor
 
//use any issue that uses the proper context/configuration for the CF you would like to update
def issueKey = "CS-2134"
def issue = ComponentAccessor.getIssueManager().getIssueObject(issueKey)
 
//to get the custom field, pass the custom field id
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(17101)
 
//get the custom field options
def optionsManager = ComponentAccessor.getOptionsManager()
def options = optionsManager.getOptions(customField.getRelevantConfig(issue))
 
//disable options
options.each {
     optionsManager.disableOption(it)
}

 

Voila! It is as simple as that! I hope these script snippets can help speed your admin tasks or client needs along. And as always, happy automating!

 

implement-optimize-atlassian-tools-CS

Recent Articles

Searching all Jira Filters for a Specific String
In this blog I will be providing yet another useful Groovy snippet to help you get the data you need from your Jira instance in a jiffy!
Amanda Kirk Amanda Kirk 4 MIN READ
Read More
Triggering a Script When a Custom Field Changes
Triggering a Script When a Custom Field Changes
At Isos, we encounter many situations where either clients or we would like to trigger a custom action or script when a custom field changes. With no out-of-the-box Jira solution, we have had to come...
Amanda Kirk Amanda Kirk 3 MIN READ
Read More
Using JQL Queries within Groovy
Using JQL Queries within Groovy
Here at Isos Technology, implementing custom Groovy scripts for Jira is commonplace. Whether it be for cleanup, complex post functions in workflows, or scripted behaviors for fields, most Jira Admins...
Amanda Kirk Amanda Kirk 3 MIN READ
Read More