How to update option value and sequence by code

JPB July 20, 2014

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

0 votes
Answer accepted
JPB September 29, 2014

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.

mark mark April 20, 2017

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.

Kevin J November 14, 2017
 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;
}
Kevin J November 14, 2017

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