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):
Is there any possibility of delete duplicate options form a custom field with a script (Script Runner)?
Therefore, the result should be:
Is this possbile via script? Has anyone done it before?
Thank you very much.
Hi Joshua,
Thank you for your response.
I'll try to clarify the problem:
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:
And after running the script:
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.