Problem running groovy from script runner

Amit Baheti May 29, 2015

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

0 votes
Answer accepted
Marc Minten _EVS_
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.
May 29, 2015

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 ?

0 votes
Amit Baheti May 29, 2015

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.

 

0 votes
Amit Baheti May 29, 2015

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?

0 votes
Marc Minten _EVS_
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.
May 29, 2015

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

Suggest an answer

Log in or Sign up to answer