Groovy: Cannot set value to "Version Picker" custom field

Hello,

In our JIRA5 setup we set "Resolved" "Version Picker" custom field. I need to set "Resolved" custom field for all existing issues. And here is the fragment of groovy script which should do this

ComponentManager componentManager = ComponentManager.getInstance()
IssueManager issueManager = componentManager.getIssueManager()

def optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
def customFieldManager = componentManager.getCustomFieldManager()
def cfResolved = customFieldManager.getCustomFieldObjectByName("Resolved")

Issue issue = issueManager.getIssueObject("PROG-1234")
def fieldResolved = cfResolved.getRelevantConfig(issue)


System.out.println("Previous Resolved On is : " + issue.getCustomFieldValue(cfResolved));

def optionResolved = optionsManager.getOptions(fieldResolved).find {it.value == "2011"}
parent.setCustomFieldValue(cfResolved, optionResolved)
System.out.println("Current Resolved On is : " + issue.getCustomFieldValue(cfResolved));

The Resolved custom field was not updated in the bug. The last println return correct "2011" value even it's not set for the bug.
In setCustomFieldValue documentation I found that
"Sets a custom field value on this Issue Object, but does not write it to the database. This is highly misleading. "

Could you please advise what method can I use in order to modify custom field value?

Thank you.

Cheers, Georgiy

1 answer

1 accepted

1 vote
Henning Tietgens Community Champion Sep 17, 2012

You could use code like this to set a custom field:

MutableIssue issue
CustomField cf
def newValue

def currentValue = issue.getCustomFieldValue(cf)
def mValue = new ModifiedValue(currentValue, newValue)
def iChangeHolder = new DefaultIssueChangeHolder()
cf.updateValue(null, issue, mValue, iChangeHolder)

// Maybe the following is not needed for 5.x
boolean wasIndexing = ImportUtils.isIndexIssues()
// be sure indexing is on
ImportUtils.setIndexIssues(true)
issueIndexManager.reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)

Henning Tietgens Community Champion Sep 17, 2012

newValue has to be the same class as the result of getCustomFieldValue (here Option).

Thank you!

I made it as you advised, i.e.

def newValue = issue.getFixVersions()
def currentValue = issue.getCustomFieldValue(cfResolved)
def mValue = new ModifiedValue(currentValue, newValue)
def iChangeHolder = new DefaultIssueChangeHolder()
cfResolved.updateValue(null, issue, mValue, iChangeHolder)

boolean wasIndexing = ImportUtils.isIndexIssues()
// be sure indexing is on
ImportUtils.setIndexIssues(true)
issueIndexManager.reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)

System.out.println("Current Resolved On is : " + issue.getCustomFieldValue(cfResolved));

and Resolved custom field contain the fix Version, but last println still shows previous currentValue. it's minor misbehavious but log contains wrong information then.

Do you know how I can get right logs, please?

Henning Tietgens Community Champion Sep 17, 2012

you have to get the issue object again, e.g. by issueManager.getIssueObject(issue.id). I think it's a good idea to do this before the reindex takes place, after the updateValue()

Thank you!

I had to get the issue object , and create new variable again after reindexing

boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
issueIndexManager.reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)

// added only for the proper log
Issue issueObjForLog = issueManager.getIssueObject(issueId)
System.out.println("Current Resolved On is : " + issueObjForLog.getCustomFieldValue(cfResolved));

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published May 30, 2018 in Marketplace Apps

Three tips for boosting your board's efficiency with Story Maps

Trello is one of the most effective tools for driving your sprints. It's customizable for every Agile team and product owners and Scrum masters (SM) love it. However, Agile teams often struggle with:...

849 views 2 9
Read article

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