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

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

Syncronize option list between 2 custom fields

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

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

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
Community showcase
Published in Jira

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across their  Jira Cloud instance.  ...

516 views 16 17
Read article

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