Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,293,827
Community Members
 
Community Events
165
Community Groups

ScriptRunner - Can I update Custom Field options and sort them alphabetically?

I am looking to use ScriptRunner to query all organisations within a SD and iterate through those values to add them to a new Custom Field. I seem to be able to query the orgs and have a large list.

A single item in the list would be: [CustomerOrganizationImpl{id=5, name=ShortName - LongName}]

I would like to take each one from the list and add to a Custom Field named, for example, "Reporting Organisation". The format of the option would be "ShortName (5) LongName". After each org is added as an option for the Custom Field I would like to sort them.

Is that possible with ScriptRunner? The only examples I have seen are related to modifying issues rather than the Custom Field values themselves.

1 answer

1 accepted

Hi,

I believe it is possible by calling sortOptionsByValue method. Here is an example where you can test in the Script Console:

import com.atlassian.jira.issue.fields.config.manager.FieldConfigManager
import com.atlassian.jira.component.ComponentAccessor

final customFieldName = "Select List"

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjects().findByName(customFieldName)
def fieldConfig = ComponentAccessor.getComponent(FieldConfigManager).getFieldConfig(10209)

ComponentAccessor.getOptionsManager().getOptions(fieldConfig).sortOptionsByValue(null)
  • Replace "Select List" with your custom field name.
  • Replace 10209 with your field config ID.

To quickly retrieve field config ID, you can navigate to Administration > Fields > Custom fields > Configure > Edit Options. Then look at the URL for 'fieldConfigId=', please refer to the screenshot below:

Screenshot 2021-06-30 at 11.32.26 AM.png

I hope this helps! :D

Cheers,
Helmy

Hi Helmy,

Thanks for your help with the sort. That was a tiny piece of the puzzle but led me to the right sources for the other pieces. I am writing a script to query all Organizations. I then take those organizations and add them as options to another custom field with the ID appended to the name of the option. The script will compare the custom field options to the Organizations and determine by the ID if the option needs renaming or if a new organization needs adding to the list. The sort happens at the end. I added it as a nightly cron job and it seems to work; I just need to iron out any bugs. 

We were previously doing this through the html using os_username and os_password to authenticate but the latest Jira updates have disabled that functionality and we weren't too keen on re-enabling it. Perhaps this info will help someone else who is looking for a similar solution to be aware it is possible with ScriptRunner.

Glad to know it worked out for you. If I did help, please accept my answer :) 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

Marketplace Partner Spotlight: AppLiger

This month the spotlight is on AppLiger. We caught up with Pavel Pavlovsky, CEO and Product Manager, to learn how the company started and what fuels the team's creativity.    Atlassian:...

336 views 0 7
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