Here is the my use case.
1. I have a "Customer Name" text field and "All Customers" single select list.
2. When issue is resolved I want to pick value present in "Customer Name" and want to add in "All Customers".
I am able to achieve this If a value to be added is already present in "All Customers". But I want to populate "All Customers" field with new value if it is not present already so that in future it is available for selection. How to do this? is it possible to do so?
and I found my answer. Here is the complete code. its jira plugin that raises on resolved event and takes value from one custom field and populate another one. Hope that helps other people.
package com.spmsoftware.plugin.listeners;
import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.event.type.EventType;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.customfields.option.Option;
import com.atlassian.jira.issue.customfields.option.Options;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.fields.config.FieldConfigScheme;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import java.util.List;
import java.util.Map;
/**
* User: adnan
* Date: 5/4/14
* Time: 4:49 PM
*/
public class IssueUpdateListener implements InitializingBean, DisposableBean {
private static final Logger LOGGER = Logger.getLogger(IssueUpdateListener.class);
private final EventPublisher eventPublisher;
// private final JiraAuthenticationContext authenticationContext;
public IssueUpdateListener(EventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
// this.authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
}
@Override
public void afterPropertiesSet() throws Exception {
eventPublisher.register(this);
}
@Override
public void destroy() throws Exception {
eventPublisher.unregister(this);
}
@EventListener
public void onIssueEvent(IssueEvent issueEvent) {
Long eventTypeId = issueEvent.getEventTypeId();
Issue issue = issueEvent.getIssue();
if (eventTypeId.equals(EventType.ISSUE_RESOLVED_ID)) {
MutableIssue mutableIssue = getMutableIssue(issue);
CustomField customerNameCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Customer Name");
CustomField allCustomersCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("All Customers");
Object customerNameVal = mutableIssue.getCustomFieldValue(customerNameCF);
Option newOptions = addOptionToCustomField(allCustomersCF, customerNameVal.toString());
LOGGER.info("New updated option {}" + newOptions);
}
}
private MutableIssue getMutableIssue(Issue issue) {
MutableIssue mutableIssue;
if (issue instanceof MutableIssue) {
mutableIssue = (MutableIssue)issue;
} else {
mutableIssue = ComponentAccessor.getIssueManager().getIssueObject(issue.getKey());
}
return mutableIssue;
}
public Option addOptionToCustomField(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);
int nextSequence = l.isEmpty() ? 1 : l.getRootOptions().size() + 1;
newOption = optionsManager.createOption(config, null, (long) nextSequence, value);
}
}
}
return newOption;
}
}