Error in Groovy script while updating radio button custom Field

Vijay Sridhar March 28, 2016

Hi

I am trying to update the CustomField using Script Runner , CustomField is Radio button Type with Default Value is No & other options are E1,E2 & E3 , while trying to execute i am getting the error , 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def targetField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Email"}
def changeHolder = new DefaultIssueChangeHolder();
targetField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(targetField), "E2"), changeHolder);

 

Error is ( same Script i was able to update with Text Field )

java.lang.ClassCastException: java.lang.String cannot be cast to com.atlassian.jira.issue.customfields.option.Option at com.atlassian.jira.issue.customfields.impl.SelectCFType.getDbValueFromObject(SelectCFType.java:68) at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.updateValue(AbstractSingleFieldType.java:146) at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:462) at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:432) at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source) at Script737.run(Script737.groovy:8)

Email_Radio_Button.png

4 answers

1 accepted

0 votes
Answer accepted
JamieA
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.
March 29, 2016

I used the following additional code in an escalation service, with no action, and it worked fine:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def indexManager = ComponentAccessor.getComponent(IssueIndexingService)

def targetField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "RadioButtons"}
def cfConfig = targetField.getRelevantConfig(issue)
def option = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'Maybe' }

def changeHolder = new DefaultIssueChangeHolder();
targetField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(targetField), option), changeHolder);
indexManager.reIndex(issue)
Vijay Sridhar March 30, 2016

yes , it's updating the Radio Buttons as expected 

Thanks 

Vijay Khacharia
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.
April 4, 2016

Yes was missing issuemanager.updateissue() or field.updatevalue() in my answer smile

1 vote
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 28, 2016

It is what it says - you are trying to set a value in a radio button field which expects an option, not a string.  The "E2" in your "set value" code needs to be a select-list option rather than a simple string.

Vijay Sridhar March 28, 2016

Hi Nic,

i tried it using select-list option but was not successful , is any sample groovy script to help in select-list option to update a custom Field ?

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 28, 2016

How are you building the option from the string that names it?

0 votes
Vijay Khacharia
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.
March 29, 2016

May be you can try to use the API function instead of the groovy clause

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
 
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
CustomField cf = customFieldManager.getCustomFieldObject("customfield_10000");
def cfConfig = cf.getRelevantConfig(issue)
 
issue.setCustomFieldValue(cf, ComponentAccessor.optionsManager.getOptions(cfConfig).getOptionForValue('Field Value', null);
Vijay Sridhar March 29, 2016

hi,

Tried it , but it is not updating the field , no errors while executing 

0 votes
Steven F Behnke
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.
March 28, 2016

As Nic indicates, you need to handle the selection as an "object" instead of a string in text. My coworker Cody recently wrote a script that did this, I think I can strip it down for you here.

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
 
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
CustomField cf = customFieldManager.getCustomFieldObject("customfield_10000");
def cfConfig = cf.getRelevantConfig(issue)
 
issue.setCustomFieldValue(cf, ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'Field Value' });
Vijay Sridhar March 28, 2016

Hi,

thanks for script , but this update the Raido button cutsomFeild Field to Null value instead of E1

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField cf = customFieldManager.getCustomFieldObject("customfield_11210");
def cfConfig = cf.getRelevantConfig(issue)

issue.setCustomFieldValue(cf, ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'E1' })

 

 

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 28, 2016

That would suggest that E1 is not a valid option for the field.  I suspect it's because your it.toString will not work for two reasons:

  • You don't usually compare strings with == unless you're 100% sure you know the format.  Try .equals instead.  e.g. String1 = "fred" then if String1.equals("fred")
  • it.toString() is going to return a string representation of the iterator, not the name of the option.  If you tried outputting the value, I think you'd probably get "[E1]" rather than "E1"
Vijay Sridhar March 29, 2016

Hi,

I make sure i have String correct by selecting the database , i tired updating Single Text Field & Radio button , but not getting reflected . 

Is there any other CustomField type which i should use in Groovy Script  .

Based on update using scriptrunner only my JQL works . 

After the update from Script runner the values are assgined as null value for the CustomField

mysql> select * from customfieldoption where CUSTOMFIELD=11405;
+-------+-------------+-------------------+----------------+----------+-------------+------------+----------+
| ID | CUSTOMFIELD | CUSTOMFIELDCONFIG | PARENTOPTIONID | SEQUENCE | customvalue | optiontype | disabled |
+-------+-------------+-------------------+----------------+----------+-------------+------------+----------+
| 11006 | 11405 | 11906 | NULL | 0 | E1 | NULL | N |
| 11007 | 11405 | 11906 | NULL | 1 | E2 | NULL | N |
| 11008 | 11405 | 11906 | NULL | 2 | E3 | NULL | N |
| 11009 | 11405 | 11906 | NULL | 3 | E0 | NULL | N |
+-------+-------------+-------------------+----------------+----------+-------------+------------+----------+
4 rows in set (0.00 sec)

 

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 29, 2016

An option object is not just a string, you need to get the option out so you can set it.  The table you've looked at here is one option per line, and you can see that it's not just "E2" on there.  E2 is just the name, the option object is the name, an id, a couple things about where the option applies, a type and a flag for disabling it.  The code you've used before will get an option, if your "if" statment was comparing correctly.   But you're comparing an option with a string.  So it's always false.

JamieA
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.
March 29, 2016

Vijar... your script looks OK to me... can you put the option in a variable called option and then log.warn (option), and see what you have in your logs.

You could use:

find { it.value == 'E1' })

rather than toString(), but it should give the same results.  Also check your E1 doesn't have any whitespace after the string.
Vijay Sridhar March 29, 2016

Hi,

 

def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {it.value == 'E2' }
log.warn (value)

Added but still same result , here is the log output 


2016-03-29 20:14:51,265 http-nio-8080-exec-13 WARN vijay.sridhar 1214x85038x1 qdzee8 10.10.99.161 /rest/scriptrunner/latest/canned/com.onresolve.scriptrunner.canned.jira.admin.EscalationService/preview [c.o.s.c.jira.utils.ConditionUtils] E2
2016-03-29 20:14:51,797 http-nio-8080-exec-13 WARN vijay.sridhar 1214x85038x1 qdzee8 10.10.99.161 /rest/scriptrunner/latest/canned/com.onresolve.scriptrunner.canned.jira.admin.EscalationService/preview [c.o.s.c.jira.utils.ConditionUtils] E2

JamieA
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.
March 29, 2016

So the option is there.

In your original post you used targetField.updateValue, but later you mentioned issue.setCustomFieldValue.

If you are not doing a transition, you need to use the former method (or IssueService.updateIssue).

Vijay Sridhar March 29, 2016

hi,

Initially i tried with targetField.updateValue & then issue.setCustomFieldValue with , both are executing without error but still it is not updating . this is the final script which i am using 

is there any specific Custom filed type i need to use ? like Label ,  Radio Button, Single Text etc for updating using  script runner ?

 

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField cf = customFieldManager.getCustomFieldObject("customfield_11210");
def cfConfig = cf.getRelevantConfig(issue)
log.debug "Need Feedback From: '"+cf+"' cleared to"
issue.setCustomFieldValue(cf, ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.value == 'E2' });
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {it.value == 'E2' }
log.warn (value)


JamieA
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.
March 29, 2016

what JIRA version and plugin version?

Vijay Sridhar March 29, 2016

Hi,

Version:Script runner = 4.2.0.7

JIRA Software 7.0.10

Steven F Behnke
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.
March 29, 2016

You know that's a fair point, my script source was written for Single Select fields. Perhaps it doesn't work with Radio Buttons. As I have time I can take a look...

Vijay Sridhar March 29, 2016

Hi,

Thanks , will wait for your update .

 

Suggest an answer

Log in or Sign up to answer