Syncronize option list between 2 custom fields

Renaud Delaplace October 6, 2021

Hello All,

I am using a custom field (single select) in our jira which contains the list of our plaforms.

Now I would like to have a second custom field (multi select) with the same list for another usage.

I am trying to figure out how I could keep both option list synced.

Any idea?

 

Thanks in advance.

1 comment

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 6, 2021

It's a little bit tricky because there are some considerations beyond ":

  1. There's no trigger to get hold of when an admin updates either list
  2. There is no way to stop admins from changing either list
  3. The options in each list will not be the same thing, there will be two options in the options table.  Imagine you've got an option called "London" for both fields.   You'll find the options table, if you look in the tables or global lists, contain things like field1:context1:London(id=10010) and field2:context1:London(id=11100)

Two of these are solvable:

  1. Have a scripted service that runs regularly, compares the two lists and updates
  2. Put logic in the scripts that does bi-directional updates (This may be a little tricky as if someone renames an option in one list, it's going to look like a delete and an add of a new one in the other list - there's no way to know its a rename)
  3. The third, there's nothing you can do about this, the options have to be separate entities.

So, as for the script, the main thing you'll need to use is optionsManager - see https://docs.atlassian.com/software/jira/docs/api/8.19.1/com/atlassian/jira/issue/customfields/manager/OptionsManager.html

Renaud Delaplace October 7, 2021

Thank you very much for your answer.

So I managed to write this groovy script which only takes care of options added in field Cffrom and recopies it to field Cfto in case it is not present. Here is the script:

import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.context.IssueContext

// SETTINGS
final def CffromName = "myfieldfrom"
final def CftoName = "myfieldto"
// END SETTINGS
def cfm = ComponentAccessor.getCustomFieldManager()
def om = ComponentAccessor.getOptionsManager()

def cffrom = cfm.getCustomFieldObjectsByName(CffromName).find()
assert cffrom : "Could not find custom field with name $CffromName"
def cfto = cfm.getCustomFieldObjectsByName(CftoName).find()
assert cfto : "Could not find custom field with name $CftoName"
def ffCfg = cffrom.getRelevantConfig(IssueContext.GLOBAL)
def localOptionsfrom = om.getOptions(ffCfg)
def ftCfg = cfto.getRelevantConfig(IssueContext.GLOBAL)
def localOptionsto = om.getOptions(ftCfg)

for (fromOptionValue in localOptionsfrom)
{
if (!localOptionsto.any { o -> o.value == fromOptionValue.value }) {
om.createOption(ftCfg, null, null, fromOptionValue.value)
}
}

 

Now the only remaining question is what is the best tool to execute it ? Is there an event that could trigger it, or is Custom scheduled job the best solution from your point of view?

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events