Problem running groovy from script runner


Below is a sample groovy script which adds build numbers to our custom field options.


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import org.apache.log4j.Category
//def Category log = Category.getInstance("com.onresolve.jira.groovy")
CustomFieldManager customFldMgr = ComponentAccessor.getCustomFieldManager();
OptionsManager optionsManager = ComponentAccessor.getComponentOfType(OptionsManager.class)
CustomField customField = customFldMgr.getCustomFieldObjectByName("Affects Build")
//Build Number Start Number
int startIndex = 501
//Build Number End Number
int endIndex = 600
def manager = ComponentAccessor.getOptionsManager()
def fieldConfig = customField.getConfigurationSchemes().listIterator().next().getOneAndOnlyConfig()
for (int i = startIndex; i <= endIndex; i++) {
    def opt = "V" + i.toString()
    manager.createOption(fieldConfig, null, null, opt)


The existing options looks like V01, V02, V03 and so on till V500 in sequence of numbers

We used to add them manually from the configuration options of the custom field.

Now we need to add 100 more versions i.e. from V501 to V600. So we created the groovy script.

The problem is it does not add the options in the sequence. 
For ex. I see V500, V510, V511, ..... V501 and so on. Notice the options are not in the order they were added by the script.




Could you please help me in figuring out the problem?

4 answers

1 accepted

Hi, strange indeed. It seems there is some parallelism...

I guess the parameter "sequence" is there to handle the order so... I would just add your iterator ( i ) as sequence ?

I think by default items are ordered alphabetically ? But the third parameter of the createOption method allows you to specify the sequence!

Thanks Marc. 

In our case the items are not sorted alphabetically. Otherwise after V10, we would have seen V101.

When we add them manually, we don't specify the sequence. It adds automatically to the bottom of the list. So I don't want to specify the sequence. The sequence in which we add, the same should be visible on GUI.

Is for loop running in parallel somehow?

Yup. Adding the sequence works. But I had to go to database and check sequence number.

In our case for V500 has the sequence number of 499. So for us it is (i -1) as the sequence number. 

Thanks Marc.


Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,789 views 11 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot