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

Stakeholder User September 15, 2021

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

3 votes
Answer accepted
Helmy Ibrahim _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 17, 2021

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

Stakeholder User September 21, 2021

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.

Like Matthias Sohn likes this
Helmy Ibrahim _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 21, 2021

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
AUG Leaders

Atlassian Community Events