Problem running groovy from script runner

Hi,

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.

Sequence-Problem.png

 

 

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 Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Wednesday in Jira

Join our webinar: How 1B+ feature flag events helped us build the new Jira

Every time you release software, there's a bit of risk – that there's a bug, that something breaks, or that the feature doesn't resonate with customers. Feature flagging helps make high stakes s...

132 views 0 3
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