I have a custom field and list of values that needs to be assigned to it from groovy. How is it possible to do?
I have tried using updateValue but it just replaces the older value and assigns the new one
custom_field.updateValue(null, mainIssue, new ModifiedValue(mainIssue.getCustomFieldValue(custom_field), values[1]),changeHolder);
import com.atlassian.jira.component.ComponentAccessor
def objects = "object or list of objects to be updated"
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield id");
MutableIssue mi = (MutableIssue) issue;
mi.setCustomFieldValue(customField , objects);
ComponentAccessor.getIssueManager().updateIssue(currentUser, mi, EventDispatchOption.DO_NOT_DISPATCH, false);
I was able to solve it like this.
The code for "update custom field" is a simple function for "put this object in that space". It can't understand what that object is as it could be anything, so it can't do clever things like "paint it purple before storage" - it can't know that there might be a function for painting things purple in any object it might be given.
Most objects that go into custom fields are simple strings, date/times, and numbers, but Jira also implements complex objects such as "options" and "users" for select lists, and on top of that, arrays for when fields can have mu
In your case, you have some form of multiple entry field, so when you put things in it, the "update custom field" is expecting an array of objects. Whatever array you give it, it's going to overwrite the current content because it cannot know that there is an add-to-array you might want to be doing.
So, let's imagine
Your code, at the moment, says "put indigo in the field". Which is too blunt and just overwrites the current value.
Your process needs to be:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Here is a method that I use to add options to a select custom field. Sorry the tabs are stripped when I paste.
public Option addOptionToCustomField(CustomField customField, String value) {
//This method either finds or creates an object in the dropdown field for the specified person.
//Either way, the object is returned so that it can be added to the container.
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 alreadyInList = false;
for(int i=0;i<l.size();i++)
{
if(l.get(i).getValue().equals(value))
{
alreadyInList = true;
newOption = l.get(i)
break;
}
}
if(!alreadyInList)
{
int nextSequence = l.isEmpty() ? 1 : l.getRootOptions().size() + 1;
newOption = optionsManager.createOption(config, null, (long) nextSequence, value);
optionsManager.disableOption(newOption)
}
}
}
}
return newOption;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.