Delete duplicate custom field options

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

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

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.

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.

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.

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. 

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.

Comment

Log in or Sign up to comment
Community showcase
Posted Sep 25, 2018 in Jira

Atlassian Research Workshop opportunity on Sep. 28th in Austin, TX

We're looking for participants for a workshop at Atlassian! We need Jira admins who have interesting custom workflows, issue views, or boards. Think you have a story to sha...

456 views 7 5
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you