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

Georgiy Senenkov September 17, 2012

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
Answer accepted
Henning Tietgens
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 17, 2012

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

Georgiy Senenkov September 17, 2012

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 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()

Georgiy Senenkov September 23, 2012

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
TAGS
AUG Leaders

Atlassian Community Events