Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to append the custom_field value from Groovy?

Kedar Kanel
Contributor
August 13, 2021

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);

 

 

3 answers

1 accepted

0 votes
Answer accepted
Kedar Kanel
Contributor
August 31, 2021
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.

1 vote
Nic Brough -Adaptavist-
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.
August 13, 2021

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

  • You have a multi-select list field to work with. 
  • Valid options are the 7 named colours of the rainbow.
  • Your issue currently has "colour = Red and Green"
  • You want to add "indigo" to the colour field

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:

  • Get current list from field (Red & Green)
  • Add indigo to the list (so - Red & Green & Indigo)
  • Put the newly updated list back into the field
0 votes
Payne
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.
August 13, 2021

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;
}

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
TAGS
AUG Leaders

Atlassian Community Events