How do I save a value into a custom field in JIRA programmatically?
Asked Answered
M

4

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?

Magyar answered 24/11, 2011 at 3:26 Comment(0)
M
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);
}
Magyar answered 25/11, 2011 at 3:5 Comment(5)
I'd do the setCustomFieldValue outside the method, so that you don't have to pin valueToSave to String.Sarmiento
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 ?Neuro
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?Radiobiology
I don't think this works anymore, and this line contains deprecated methods: ComponentManager.getInstance().getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem( customField);Twila
docs.atlassian.com/software/jira/docs/api/7.6.1/… the ComponentManager is replaced by ComponentAccessor docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/… so: ComponentAccessor.getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem(customField);Concise
W
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.

Whipstall answered 28/8, 2012 at 15:23 Comment(3)
What if you have to populate a select list custom field and the new value is not present in the select list?Radiobiology
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...Fleischman
The fieldLayoutItem is used to determine the renderer - in case it is null it will fallback to the text renderer.Theotheobald
T
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);
Theotheobald answered 21/8, 2015 at 15:31 Comment(0)
B
-1

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

Bionics answered 3/7, 2012 at 9:55 Comment(1)
The link is broken but the add-on still exists as the commonly used JIRA Suite UtilitiesZeralda

© 2022 - 2024 — McMap. All rights reserved.