Custom field update is not working using Jira API

I have a date picker customfield and used below code to create/update it is value,

CustomField customField1 = customFieldManager.getCustomFieldObject("customfield_13961");
FieldLayoutItem fieldLayoutItem = ComponentManager.getInstance().getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem(customField1);
DefaultIssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder();
customField1.updateValue(fieldLayoutItem, issue, new ModifiedValue(issue.getCustomFieldValue(customField1),date), issueChangeHolder);

I update the custom field in Issue resolve screen. The value is not updated. ChangeItem has the entered value in OldValue, not in NewValue. No records in CustomFieldValue table.

But when I reopen it, it is updated(the same logic is called again) and displayed in issue screen.

Any ideas?

3 answers

This widget could not be displayed.

Hi Mani,

Here are the general steps again. You have most of it but you are missing the field.store() . Give it another try

@Override
    public Map<String, Object> getVelocityParameters(final Issue issue,final CustomField field,final FieldLayoutItem fieldLayoutItem) {
        final Map<String, Object> map = super.getVelocityParameters(issue, field, fieldLayoutItem);

        if (issue == null) {
            return map;
        }
        
        DefaultIssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder();
        
        FieldConfig fieldConfig = field.getRelevantConfig(issue);
        try
            {                                
                field.updateValue(fieldLayoutItem, issue, new ModifiedValue(field.getValue(issue), "My new value"), issueChangeHolder);
                field.store();
            }
            catch(Exception exc)
            {
                //Log error
            }
        return map;
    }

Cheers

Bhushan

Bhushan, thanks for your answer. I tried below code as suggested,

CustomField customField1 = customFieldManager.getCustomFieldObject("customfield_13961");
FieldLayoutItem fieldLayoutItem = ComponentManager.getInstance().getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem(customField1);
FieldConfig fieldConfig = customField1.getRelevantConfig(issue);

DefaultIssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder();
customField1.updateValue(fieldLayoutItem, issue, new ModifiedValue(issue.getCustomFieldValue(customField1),date), issueChangeHolder);
customField1.store();

But it didn't get updated. Do I miss anything?

Any updates would be helpful.

Can you post the full code?

Sure, I will post the code.

Hi Bhushan,

Please find the full code. I excute this code while resolving an issue (CommentAssignIssue.java),

//Get fix version's release date
VersionManager versionManager1 = ComponentManager.getComponent(VersionManager.class);
Version version = versionManager1.getVersion(fixVersions);
Date fixReleaseDate = null;
if (null != version.getReleaseDate()){
fixReleaseDate=version.getReleaseDate();
}
//Get Custom field where fix release date will be set
CustomField customField1 = customFieldManager.getCustomFieldObject("customfield_13961");
FieldLayoutItem fieldLayoutItem = ComponentManager.getInstance().getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem(customField1);
FieldConfig fieldConfig = customField1.getRelevantConfig(issue);
DefaultIssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder();
customField1.updateValue(fieldLayoutItem, issue, new ModifiedValue(issue.getCustomFieldValue(customField1),fixReleaseDate), issueChangeHolder);
customField1.store();
//customFieldManager.updateCustomField(customField1);

This widget could not be displayed.

hey! try with the following code

CustomField customField1 = customFieldManager.getCustomFieldObject("customfield_13961");
 FieldLayoutManager fieldLayoutManager = ComponentManager.getInstance().getFieldLayoutManager();
FieldLayout layout = fieldLayoutManager.getFieldLayout(issue.getProjectObject(), issue.getIssueTypeObject().getId());
if (layout.getId() == null) {
     layout = fieldLayoutManager.getEditableDefaultFieldLayout();
}
FieldLayoutItem fieldLayoutItem =layout.getFieldLayoutItem(customField1.getId());
DefaultIssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder();
issue.setCustomFieldValue(customField1, newValue);
customField1.updateValue(fieldLayoutItem, issue, new ModifiedValue(issue.getCustomFieldValue(customField1),date), issueChangeHolder);

Thanks for your help. I will test it and let you know.

Hi Rambanam,

The code you suggested didn't work.

MutableIssue mIssue = (MutableIssue)issue;
CustomField customField1 = customFieldManager.getCustomFieldObject("customfield_13961");
FieldLayoutManager fieldLayoutManager = ComponentManager.getInstance().getFieldLayoutManager();
FieldLayout layout = fieldLayoutManager.getFieldLayout(issue.getProjectObject(), issue.getIssueTypeObject().getId());
if (layout.getId() == null) {
layout = fieldLayoutManager.getEditableDefaultFieldLayout();
}
FieldLayoutItem fieldLayoutItem =layout.getFieldLayoutItem(customField1.getId());
DefaultIssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder();
mIssue.setCustomFieldValue(customField1, date);

customField1.updateValue(fieldLayoutItem, issue, new ModifiedValue(mIssue.getCustomFieldValue(customField1),date), issueChangeHolder);

}

Please let me know if you have any other approaches.

It would be very helpful, if anyone could guide me right direction.

I couldn't figure it out. Is there anything to resolve it?

what type of Custom field you are trying to update and what is the error message you are getting/showing on log

Custom field type is Date Picker. There is no error raised. But above code does not create an entry in database.

This widget could not be displayed.
try with the following code, it is working for me

CustomField customField1 = customFieldManager.getCustomFieldObject("customfield_13961"); FieldLayoutManager fieldLayoutManager = ComponentManager.getInstance().getFieldLayoutManager(); FieldLayout layout = fieldLayoutManager.getFieldLayout(issue.getProjectObject(), issue.getIssueTypeObject().getId()); if (layout.getId() == null) { layout = fieldLayoutManager.getEditableDefaultFieldLayout(); } FieldLayoutItem fieldLayoutItem =layout.getFieldLayoutItem(customField1.getId()); DefaultIssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder(); Calendar calendar = Calendar.getInstance(); Date today = new java.sql.Timestamp(calendar.getTime().getTime()); issue.setCustomFieldValue(customField1, today); customField1.updateValue(fieldLayoutItem, issue, new ModifiedValue(today,today), issueChangeHolder);

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

97 views 1 0
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you