How to update option value and sequence by code

Hi

I'm trying to update the sequence of an option I have in the system. I'm using the OptionsManager class to get the options associated to the field configuration, and then by 'id' I get the option I need to update; that's working fine. But when I try to update the sequence value with the method 'setSequence(Long sequence)', the value doesn't change, but no error or exception is shown. if I use the method 'getSequence()' with the same option object, that's working fine, I get the actual sequence value. I tried the same with the option value, and the same problem, I cannot change the value (with 'setValue'), but can read it (with 'getValue'). To summarize, the getter methods are working, but the setter methods are not working for the Option object.

P.D: When I create an Option with the method 'createOption(fieldConfig, parentid, sequence, value)' of the class OptionsManager, that's working fine, the option is added with the values, but my problem is when I need to update the sequence of that option.

Thank you in advance for any help.

1 answer

1 accepted

This widget could not be displayed.

Finally, after a lot of time testing, I found a solution to that issue, I write it here for future references if someone has a similar problem.

Those setters methods I talk about in the previous question were working when JIRA was active (my development environment), but when I shut down the instance, the changes made to the option were not saved (the database wasn't modified). But after some tests, I found that to make those changes persistent, I had to add this line after the call to the setter method:

optionsManager.updateOptions('optionsVariable');

With this line, the changes were saved in the database and everything was ok.

I tried the following code and it did not save for me.

Options options = optionsManager.getOptions(config);
Option last = options.get(options.size()-1);
Option option = optionsManager.createOption(config, last.getOptionId(), last.getSequence() + 1, "4.0"); List x = new ArrayList(); x.add(option); optionsManager.updateOptions(x);

Also tried this but again it did not save:

Options options = optionsManager.getOptions(config);
Option last = options.get(options.size()-1);
Option option = optionsManager.createOption(config, last.getOptionId(), last.getSequence() + 1, "4.0");
options.add(option);
optionsManager.updateOptions(options);

When I look at the drop down I do not see the new value.

 public static Option addOptionToSelectList(CustomField customField, String value) {

Option newOption = null;

if (customField != null) {
List<FieldConfigScheme> schemes = customField
.getConfigurationSchemes();
if (schemes != null && !schemes.isEmpty()) {
FieldConfigScheme sc = schemes.get(0);
Map configs = sc.getConfigsByConfig();
if (configs != null && !configs.isEmpty()) {
FieldConfig config = (FieldConfig) configs.keySet()
.iterator().next();
OptionsManager optionsManager = ComponentAccessor
.getOptionsManager();
Options l = optionsManager.getOptions(config);
boolean addOption = true;
for (Option opt : l) {
String optionValue = opt.getValue().trim();

if (optionValue.equalsIgnoreCase(value.trim())) {
addOption = false;

}
}
if (addOption) {
int nextSequence = l.isEmpty() ? 1 : l.getRootOptions()
.size() + 1;
newOption = optionsManager.createOption(config, null,
(long) nextSequence, value);
l.sortOptionsByValue(null);

}

}
}
}

return newOption;
}

It didn't pick up the formatting :/  But i use this code to add a value to a Select List

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Aug 06, 2018 in Jira Service Desk

A is for Activate: Share your top Jira Service Desk onboarding tips for new users!

Hi, everyone! Molly here from the Jira Service Desk Product Marketing Team :).  In the spirit of this month's  august-challenge, we're sourcing stories of Jira Service Desk activation fro...

536 views 23 15
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