Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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
Highlighted

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.

@Joshua Yamdogo _ Adaptavist Could you share your code?

Comment

Log in or Sign up to comment
TAGS

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