Try to add the values of multi-select type customField

@EventListener
public void onIssueEvent(IssueEvent issueEvent) {
    Long eventTypeId;
    eventTypeId = issueEvent.getEventTypeId();
    Issue issue = issueEvent.getIssue();
    System.out.println("123134445");
    if (eventTypeId.equals(EventType.ISSUE_CREATED_ID)) {
        MutableIssue mutableIssue = getMutableIssue(issue);
        //CustomField customerNameCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Customer Name");
        CustomField allCustomersCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("我的宝贝");
        System.out.println(allCustomersCF+"dddff");
        //  Object customerNameVal = mutableIssue.getCustomFieldValue(customerNameCF);
        Option newOptions = addOptionToCustomField(allCustomersCF, "123");
        System.out.println("New updated option {}" + newOptions);
    }
    System.out.println("huhuhuh");
}

private MutableIssue getMutableIssue(Issue issue) {
    LOGGER.info("jkjkjkjjjkjk");
    System.out.println("rrrwe4");
    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()) {
            System.out.println("schemes !=null");
            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;

}

this is my test code to add the values of multi-select type customField ,and it really worked fine through a issue created, but I try to find anther better way to do it. Is there any better event to complete this mission?

1 Like