Error in Groovy script while updating radio button custom Field

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

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)

yes , it's updating the Radio Buttons as expected 

Thanks 

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

1 votes

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.

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 ?

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

0 votes
Steven Behnke Community Champion Mar 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' });

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' })

 

 

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"

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)

 

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.

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.

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

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).

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)


what JIRA version and plugin version?

Hi,

Version:Script runner = 4.2.0.7

JIRA Software 7.0.10

Steven Behnke Community Champion Mar 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...

Hi,

Thanks , will wait for your update .

 

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);

hi,

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

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,864 views 12 18
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot