It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to change the value of a custom field using Java?

ryan McCullough Mar 15, 2012

I'm trying to edit a few custom fields on a custiom issue type. I have an array of keys for issues, I've queried for the issues, now how in the world do modify the values of the custom fields I'm targeting?

I'm a complete newbe here, so please be gentile :)

Thanks,

Ryan

2 answers

1 accepted

4 votes
Answer accepted

Hi Ryan, here's a simplified example:

//get custom field
FieldManager fieldManager = ComponentManager.getInstance().getFieldManager();
String field = "customfield_10000";
CustomField myCustomField = fieldManager.getCustomField(field);

//read value
Object val = myCustomField.getCustomFieldType().getValueFromIssue(myCustomField, issue);
String customFieldValue = val == null ? "" : val.toString();

//write data to custom field
ArrayList<String> customFieldNewContent = new ArrayList<String>();
customFieldNewContent.add("new value");
this.persister.updateValues(fieldStart, issue.getId(), PersistenceFieldType.TYPE_LIMITED_TEXT, customFieldNewContent);

Good luck!

ryan McCullough Mar 15, 2012

Thanks a million Dennis :D

ryan McCullough Mar 15, 2012

Oh! one more thing, what is the persister reference...

I don't see a type declaration, and I didnt have any luck looking for Persisters...

this.persister.updateValues(fieldStart, issue.getId(), PersistenceFieldType.TYPE_LIMITED_TEXT, customFieldNewContent);

thanks again :)

Mea culpa, you need to add the persister to your constructor, here's a snippet from my code:

public class IssueUpdatedListener implements InitializingBean, DisposableBean {
 
    private static final Logger log = LoggerFactory.getLogger(IssueUpdatedListener.class);
 
    private final EventPublisher eventPublisher;

	private final CustomFieldValuePersister persister;
     
    /**
     * Constructor.
     * @param eventPublisher injected {@code EventPublisher} implementation.
     */
    public IssueUpdatedListener(EventPublisher eventPublisher,CustomFieldValuePersister persister) {
        this.eventPublisher = eventPublisher;
        this.persister = persister;   // <==== this is what you need
    }

Also, be sure to have the following imports:

import com.atlassian.jira.issue.customfields.persistence.CustomFieldValuePersister;
import com.atlassian.jira.issue.customfields.persistence.PersistenceFieldType;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.FieldManager;
Sergey Tverdokhleb Feb 04, 2013

And what is fieldStart? It doesn't have any declaration as well.

0 votes
Roman Samorodov Jan 16, 2017

For those who are searching for answer that is up to date in 2017.

Answer is HERE.

marko divjak Apr 07, 2017

Can you fix link to answer ? 

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted in Jira Software

Demo Den Ep. 5: Meet Jira Mobile

See how to use Jira on the go! Demo Den Episode 5 is here: meet Jira Mobile with  Jira  Product Manager Rayen Magpantay.  Demo Den is in our monthly series where a Jira PM demonstrates...

172 views 1 7
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you