How to set flag on linked issue during scriptrunner clone issue

I am trying to clone an issue to another project and also set the Flagged custom field to "Impediment" on the cloned issue:

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Flagged")
def fieldConfig = cf.getRelevantConfig(issue)
def optionsManager = ComponentAccessor.getOptionsManager()
def value = optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Impediment' }
issue.setCustomFieldValue(cf, value)

When I execute this code, I see an exception in the JIRA logs:

2016-03-29 15:42:34,210 http-bio-8080-exec-13 ERROR matt_shelton 942x5658x1 iod9tz 0:0:0:0:0:0:0:1 /secure/QuickCreateIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: CAPD-31, actionId: 1, file: null
com.atlassian.jira.exception.CreateException: Error occurred while creating issue. This could be due to a plugin being incompatible with this version of JIRA. For more details please consult the logs, and see: http://confluence.atlassian.com/x/3McB
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:757)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:645)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssueObject(DefaultIssueManager.java:770)
	at com.atlassian.jira.issue.IssueManager$createIssueObject$0.call(Unknown Source)
	at com.onresolve.scriptrunner.canned.jira.utils.AbstractCloneIssue.doScript(AbstractCloneIssue.groovy:96)
	at com.onresolve.scriptrunner.canned.jira.utils.CopyIssueWithAttachments.super$2$doScript(CopyIssueWithAttachments.groovy)
	at com.onresolve.scriptrunner.canned.jira.utils.CopyIssueWithAttachments.doScript(CopyIssueWithAttachments.groovy:10)
	at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue.super$3$doScript(CloneIssue.groovy)
	at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue.doScript(CloneIssue.groovy:85)
Caused by: com.atlassian.jira.workflow.WorkflowException: Error occurred while creating issue. This could be due to a plugin being incompatible with this version of JIRA. For more details please consult the logs, and see: http://confluence.atlassian.com/x/3McB
	at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:923)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:746)
	... 8 more
Caused by: java.lang.ClassCastException: com.atlassian.jira.issue.customfields.option.LazyLoadedOption cannot be cast to java.util.Collection
	at com.atlassian.jira.issue.customfields.impl.AbstractMultiCFType.createValue(AbstractMultiCFType.java:41)
	at com.atlassian.jira.issue.fields.CustomFieldImpl.createValue(CustomFieldImpl.java:854)
	at com.atlassian.jira.workflow.function.issue.IssueCreateFunction.execute(IssueCreateFunction.java:88)
	at com.opensymphony.workflow.AbstractWorkflow.executeFunction(AbstractWorkflow.java:1050)
	at com.opensymphony.workflow.AbstractWorkflow.transitionWorkflow(AbstractWorkflow.java:1446)
	at com.opensymphony.workflow.AbstractWorkflow.initialize(AbstractWorkflow.java:615)
	at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:886)
	... 9 more

Based on the exception, I suspect the issue here is how I'm populating value, but I don't know how else to get it. The line in question was based on this answer, but I may be misunderstanding it.

Thanks!

2 answers

This widget could not be displayed.

The error is here:

Caused by: java.lang.ClassCastException: com.atlassian.jira.issue.customfields.option.LazyLoadedOption cannot be cast to java.util.Collection

This is because Flagged is (normally) a check box field and takes a Collection of Option objects.

So changing the last line to:

issue.setCustomFieldValue(cf, [value])

should work.

This widget could not be displayed.

Attempting to get value this way also does not work, despite this being, I think, the more direct route:

def value = optionsManager.getOptions(fieldConfig).getOptionById(10000)

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

292 views 5 0
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