Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to groovy update a multi checkbox field during post-function script

Robert Poldervaart June 21, 2012

I am trying to write a groovy script to update (set) a checkbox field during a post-function. So far, I can't get anything to work. I have a custom checkbox field with one option, 'Yes'. I simply want to check that check box during a post-function groovy script.

To start, I have:

CustomFieldManager cfManager = ComponentManager.getInstance().getCustomFieldManager()
        CustomField cfFixed = cfManager.getCustomFieldObjectByName("Fixed in Trunk")
        IssueChangeHolder changeHolder = new DefaultIssueChangeHolder()

I'm trying this with the following:

issue.setCustomFieldValue(cfFixed, 'Yes')

But I get exceptions that it can't cast the string 'Yes' to an appropriate option.

I'm trying this:

cfFixed.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfFixed),'Yes'),changeHolder)

But continue with exceptions. I'm just not sure what type of object I need to pass into setCustomFieldValue or ModifiedValue.

Even when I do get the code to run by creating an Option list with 'Yes', it doesn't actually update the field... Do I have to do issue.store() or something to commit the update?

This is getting exhausting for what seems like such a simple task. Any help is greatly appreciated.


Robert

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Dieter
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.
June 23, 2012
As you noticed, you cannot set the value 'Yes' directly. This has changed in Jira 4.4 and you must pass an object implementing the interface Option now. The following should work though untested:
OptionsManager optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
fieldConfig = cfFixed.getRelevantConfig(issue)
yes = [ optionsManager.getOptions(fieldConfig).find("Yes")]
cfTarget.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfTarget), yes), changeHolder)
Robert Poldervaart June 25, 2012

Hi Dieter, Thanks so much for the help. Here is what I now have. I have two questions:

1) I'm still getting an exception instantiating yes. It doesn't find a signature for the find method for a String.

2) And in your code, you have cfTarget. Did you mean cfFixed, which is the field that I'm trying to set?

CustomFieldManager cfManager = ComponentManager.getInstance().getCustomFieldManager()
        CustomField cfFixed = cfManager.getCustomFieldObjectByName("Fixed in Trunk")
        IssueChangeHolder changeHolder = new DefaultIssueChangeHolder()

        OptionsManager optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
        FieldConfig fieldConfig = cfFixed.getRelevantConfig(issue)
        Option yes = [ optionsManager.getOptions(fieldConfig).find("Yes")]
        cfFixed.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfFixed), yes), changeHolder)

Dieter
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.
June 25, 2012

Hi Robert,

Don't know what happened , but the comment was completely garbled when i edited it on my iPad.

Now hopefully this is correct:

CustomFieldManager cfManager = ComponentManager.getInstance().getCustomFieldManager()
CustomField cfFixed = cfManager.getCustomFieldObjectByName("Fixed in Trunk")
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder()
 
OptionsManager optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
FieldConfig fieldConfig = cfFixed.getRelevantConfig(issue)
yes = [ optionsManager.getOptions(fieldConfig).getOptionForValue("Yes",null)]
cfFixed.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfFixed), yes), changeHolder)

Dieter
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.
June 25, 2012
Hi Robert,

sorry, it's


1
2
3
4
5
6
7
8
CustomFieldManager cfManager = ComponentManager.getInstance().getCustomFieldManager()
CustomField cfFixed = cfManager.getCustomFieldObjectByName("Fixed in Trunk")
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder()
 
OptionsManager optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
FieldConfig fieldConfig = cfFixed.getRelevantConfig(issue)
Option yes = [ optionsManager.getOptions(fieldConfig).getOptionForValue("Yes",null)]]
cfFixed.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfFixed), yes), changeHolder)
Dieter
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.
June 25, 2012
Hi Robert,

sorry, it's


1
2
3
4
5
6
7
8
CustomFieldManager cfManager = ComponentManager.getInstance().getCustomFieldManager()
CustomField cfFixed = cfManager.getCustomFieldObjectByName("Fixed in Trunk")
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder()
 
OptionsManager optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
FieldConfig fieldConfig = cfFixed.getRelevantConfig(issue)
Option yes = [ optionsManager.getOptions(fieldConfig).getOptionForValue("Yes",null)]]
cfFixed.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfFixed), yes), changeHolder)
Dieter
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.
June 25, 2012
Hi Robert,

sorry, it's


1
2
3
4
5
6
7
8
CustomFieldManager cfManager = ComponentManager.getInstance().getCustomFieldManager()
CustomField cfFixed = cfManager.getCustomFieldObjectByName("Fixed in Trunk")
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder()
 
OptionsManager optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
FieldConfig fieldConfig = cfFixed.getRelevantConfig(issue)
Option yes = [ optionsManager.getOptions(fieldConfig).getOptionForValue("Yes",null)]]
cfFixed.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfFixed), yes), changeHolder)
Robert Poldervaart June 25, 2012

Hi. Thanks again! It took me a few tries, but I got it to work. I had to add ArrayList yes = ... where you just had yes = ... I had tried Option and Options, and then based on the cast exception that it was throwing, I tried ArrayList.

ArrayList yes = [ optionsManager.getOptions(fieldConfig).getOptionForValue("Yes",null)]

It is updating my field now. Thank you very much!


Robert

TAGS
AUG Leaders

Atlassian Community Events