How do I save a value into a custom field in JIRA

2019-02-04 14:27发布

I've spent days trying to find out how to save or update a value into a CustomField programmatically and finally found out how it's done. So I'll make this a question and then answer it as I would have loved to have this question and answer.

There is conflicting documentation on how to save or update a value for a Custom Field in JIRA. I was using:

customField.setCustomFieldValue(CustomField, value);

This does not save the value into the database but it does update the value as far as I can tell. It's only useful if you are using the CustomField further down in a Workflow Post Function transition for example.

I'm using Jira 4.3.2.

How do I persist the the CustomFields value into the JIRA database?

4条回答
干净又极端
2楼-- · 2019-02-04 14:49

I had the same issue and had it resolved using this plugin, fyi=)

查看更多
SAY GOODBYE
3楼-- · 2019-02-04 14:56

Here is how I do it (for a custom field I programmatically store a random UUID in):

CustomField cfHash = customFieldManager.getCustomFieldObjectByName(...);
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
try {
    Object newHashValue = java.util.UUID.randomUUID().toString();
    Object oldHashValue = issue.getCustomFieldValue(cfHash);

    issue.setCustomFieldValue(cfHash, newHashValue);
    cfHash.updateValue(null, issue, new ModifiedValue(oldHashValue, newHashValue), changeHolder);
...

More or less the same as you but with another way to get the ModifiedValue-Object.

查看更多
趁早两清
4楼-- · 2019-02-04 15:03

Ok, this is how I'm successfully updating and saving the CustomField value into the JIRA db.

Comments welcome...

private void saveValue(MutableIssue issue, String valueToSave, CustomField
        customField) throws FieldLayoutStorageException {

    issue.setCustomFieldValue(customField, valueToSave);

    Map<String, ModifiedValue> modifiedFields = issue.getModifiedFields();

    FieldLayoutItem fieldLayoutItem =
    ComponentManager.getInstance().getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem(
            customField);

    DefaultIssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder();

    final ModifiedValue modifiedValue = (ModifiedValue) modifiedFields.get(customField.getId());

    customField.updateValue(fieldLayoutItem, issue, modifiedValue, issueChangeHolder);
}
查看更多
Lonely孤独者°
5楼-- · 2019-02-04 15:07

Here a solution that works for me in JIRA 6.4.7 to update a custom field value. Actually Im updating a single select field, therefore I have to get the Option for it:

MutableIssue issue = issueManager.getIssueByCurrentKey(issueKey); 
FieldConfig relevantConfig = customField.getRelevantConfig(issue);
// if you use a text field use String. or double for numeric
Option optionForValue = optionsManager.getOptions(relevantConfig).getOptionForValue(option, null);
issue.setCustomFieldValue(customField,optionForValue);
Map<String, ModifiedValue> modifiedFields = issue.getModifiedFields();
FieldLayoutItem fieldLayoutItem =
fieldLayoutManager.getFieldLayout(issue).getFieldLayoutItem(customField);
DefaultIssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder();
final ModifiedValue modifiedValue = modifiedFields.get(customField.getId());
customField.updateValue(fieldLayoutItem, issue, modifiedValue, issueChangeHolder);
查看更多
登录 后发表回答