Delete duplicate custom field options

Miguel Ángel García Gómez November 13, 2017

Hi all!

We have a 7.3.8 Jira Server with some custom fields. One of these has some options created automatically. Unfortunately, in this process there was a mistake and some options were created duplicate.

So now, the options of this custom field are (values are simplify to clarify the problem):

  • Option 1
  • Option 1
  • Option 2
  • Option 2

Is there any possibility of delete duplicate options form a custom field with a script (Script Runner)?

Therefore, the result should be:

  • Option 1
  • Option 2

Is this possbile via script? Has anyone done it before? 

Thank you very much.

1 comment

Joshua Yamdogo @ Adaptavist
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 14, 2017

Hi Miguel,

I understand your problem, but could you clarify the details surrounding your use case? I mean, do you plan to just run this script once? Do you need to fix just one field that has duplicate options or are there multiple fields that have duplicate options? 

I am assuming this is just a custom field of type Select List. If there aren't many duplicate values, can't you simply go to the "Edit" page for the custom field and manually delete the duplicate options without using a script?

Regards,

Josh

Miguel Ángel García Gómez November 14, 2017

Hi Joshua,

Thank you for your response. 

I'll try to clarify the problem:

  • I plan to run this script once, but probably the problem would reproduce in a future.
  • At the moment, I need to fix one field, but in the future could be more than one.
  • Yes, it's a custom field of type Select List and has a a lot of options (thousands), so manually deleting is not an option.

I hope this clarify the scenario.

Thanks in advance ;)

Regards,

Miguel.

Joshua Yamdogo @ Adaptavist
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 16, 2017

Hi Miguel,

I developed a script to be used in the Script Console to delete duplicate option values for a given Select List Field.

Here are my field options before the script: 

Screen Shot 2017-11-16 at 1.01.53 PM.png

 

And after running the script:

Screen Shot 2017-11-16 at 1.02.15 PM.png

Here is the script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.issue.context.IssueContextImpl
import com.atlassian.jira.issue.customfields.option.Options

def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def field = customFieldManager.getCustomFieldObjectByName("TestField")
IssueContextImpl issueContext = new IssueContextImpl(null, null)
FieldConfig fieldConfig = field.getRelevantConfig(issueContext)
Options options = optionsManager.getOptions(fieldConfig)
def uniqueOptions = []
def optionsClone = options.clone()
for (option in optionsClone) {
if (!uniqueOptions.contains(option.getValue())) {
uniqueOptions.add(option.getValue())
}
else {
log.debug("Found duplicate option: " + option.getValue())
options.removeOption(option)
}
}

Just replace the field name in quotes with your field name and run the script in the Script Console. You may have static type checking errors, but it will still work OK.

Miguel Ángel García Gómez November 21, 2017

Thank you very much Joshua for your response and your effort.

It have been really helpful, so I appreciate your help ;)

However, I have another doubt now. What happens with the duplicated options that are currently related with an issue? For example, If an issue A is related with option "Option 1" (with an imaginary ID 1), and you delete this option becaue exists another option "Option 1" (with ID 2), it is possible to reassign the issue to use "Option 1" with ID 2 (the option that has not been removed).

Thanks a lot!

Regards,

Miguel.

Joshua Yamdogo @ Adaptavist
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 21, 2017

To be quite frank Miguel, I am not sure what will happen. I suspect you'll end up with a lot of issues that have empty values for that field. Yes, I can see how that would be problematic if you deleted options that issues were actually using. 

You could definitely search through issues and replace their duplicated option with the correct option before running the deletion script. I have a script to search for issues and replace their custom field values.

But how many unique options are there for this field? If you had 10 unique options, this wouldn't take very long. If you have hundreds or thousands of unique options, I'd think this would become very time consuming. 

Miguel Ángel García Gómez November 21, 2017

Hi Joshua,

It would be great if you can share the script that you have mentioned :)

Talking about the unique options, I'm afraid that there are thousands of unique options, and probably, as you said, it could take a long time.

Thanks.

Regards,

Miguel.

Q November 20, 2019

Joshua Yamdogo @ Adaptavist Could you share your code?

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events