Blog - Isos Technology

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

Written by Amanda Kirk | Sep 17, 2020

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!