14

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?

tobii
  • 517
  • 3
  • 13
enormace
  • 641
  • 2
  • 6
  • 20

4 Answers4

17

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);
}
lilalinux
  • 2,903
  • 3
  • 43
  • 53
enormace
  • 641
  • 2
  • 6
  • 20
  • I'd do the setCustomFieldValue outside the method, so that you don't have to pin valueToSave to String. – lilalinux Sep 26 '12 at 12:30
  • I am asking one question here itself as I don't want to create a duplicate. Using above solution I am able to save the custom field data into database but the custom field value is not appearing on the view issue page when creating an issue? But it is shown when page is refreshed !! I am sure why this extra refresh is required ? Can you highlight some on this ? – Sukane Nov 18 '13 at 09:59
  • I have one question. what if I don't want to change value to the issue but I only want to update options of one custom field. I mean picking value from one custom field and updating to another? – Mohammad Adnan Apr 05 '14 at 18:38
  • I don't think this works anymore, and this line contains deprecated methods: `ComponentManager.getInstance().getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem( customField);` – JBaczuk Mar 04 '15 at 14:53
  • https://docs.atlassian.com/software/jira/docs/api/7.6.1/index.html?index-all.html the ComponentManager is replaced by ComponentAccessor https://docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/jira/component/ComponentAccessor.html so: ComponentAccessor.getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem(customField); – yan Jun 25 '18 at 07:06
3

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.

jonsca
  • 10,218
  • 26
  • 54
  • 62
  • What if you have to populate a select list custom field and the new value is not present in the select list? – Mohammad Adnan Apr 05 '14 at 19:13
  • So, the `fieldLayoutItem`-argument of `updateValue` can just be null? I wonder, what it means -- and why is there no form of `updateValue` without that argument at all... – Mikhail T. Apr 02 '15 at 19:35
  • The `fieldLayoutItem` is used to determine the renderer - in case it is null it will fallback to the text renderer. – LukeSolar Aug 21 '15 at 15:15
0

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);
LukeSolar
  • 3,795
  • 4
  • 32
  • 39
-1

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

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
Shihwei
  • 68
  • 1
  • 5
  • The link is broken but the add-on still exists as the commonly used JIRA Suite Utilities – mdoar Mar 10 '16 at 00:55