Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Creating multiple issues based on selected values in a multiselect field

Khushbu Jowaheer August 21, 2018

Hello Team,

 

I am trying to automatically create multiples issues based on the selected options from a multi select custom field.

 

Please find my code below: 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

Issue issue = issue
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
int i = 0
//assuming this is a multiple select list custom field
def suppliers = ComponentAccessor.getCustomFieldManager().getCustomFieldObject('customfield_13401')
def selectedSuppliers = issue.getCustomFieldValue(suppliers) as Collection
def issueSummary = issue.summary
def issueDescription = issue.description
def issueReporter = issue.reporter

selectedSuppliers.each {
def issueFactory = ComponentAccessor.getIssueFactory()
def issueManager = ComponentAccessor.getIssueManager()
def newIssue = issueFactory.cloneIssue(issue)

newIssue.setIssueTypeId("11800")
newIssue.setSummary(issueSummary)
newIssue.setDescription(issueSummary)
newIssue.setReporter(issueReporter)
newIssue.setCustomFieldValue(suppliers, it.toString())

Map<String,Object> newIssueParams = [issue:newIssue] as Map<String,Object>
issueManager.createIssueObject(currentUser, newIssueParams)
}

 

Unfortunately I am getting this error:

Please help!!

2018-08-21 16:30:01,878 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-08-21 16:30:01,880 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: CR-425, actionId: 471, file: <inline script>
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:588)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:494)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssueObject(DefaultIssueManager.java:599)
	at com.atlassian.jira.issue.managers.RequestCachingIssueManager.createIssueObject(RequestCachingIssueManager.java:198)
	at com.atlassian.jira.issue.IssueManager$createIssueObject$2.call(Unknown Source)
	at Script279$_run_closure1.doCall(Script279.groovy:26)
	at Script279.run(Script279.groovy:14)
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:766)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:580)
	... 6 more
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Collection
	at com.atlassian.jira.issue.customfields.impl.AbstractMultiCFType.createValue(AbstractMultiCFType.java:39)
	at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
	at com.atlassian.jira.workflow.function.issue.IssueCreateFunction.execute(IssueCreateFunction.java:81)
	at com.opensymphony.workflow.AbstractWorkflow.executeFunction(AbstractWorkflow.java:1014)
	at com.opensymphony.workflow.AbstractWorkflow.transitionWorkflow(AbstractWorkflow.java:1407)
	at com.opensymphony.workflow.AbstractWorkflow.initialize(AbstractWorkflow.java:606)
	at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:742)
	... 7 more

 

 

2 answers

1 accepted

1 vote
Answer accepted
Nir Haimov
Community Champion
August 21, 2018

Hi,

Just for test purpose, try to simply create 1 task without your customfield and collection.

Like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

Issue issue = issue
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
int i = 0
/*assuming this is a multiple select list custom field/*
def issueSummary = issue.summary
def issueDescription = issue.description
def issueReporter = issue.reporter

def issueFactory = ComponentAccessor.getIssueFactory()
def issueManager = ComponentAccessor.getIssueManager()
def newIssue = issueFactory.cloneIssue(issue)

newIssue.setIssueTypeId("3")
newIssue.setSummary(issueSummary)
newIssue.setDescription(issueSummary)
newIssue.setReporter(issueReporter)

Map<String,Object> newIssueParams = [issue:newIssue] as Map<String,Object>
issueManager.createIssueObject(currentUser, newIssueParams)

 

See if it creates you an issue or not.

If it creates, then i think your exception is somewhere with your collection, maybe you are doing something wrong there.

Khushbu Jowaheer August 22, 2018

Hello Nic,

I did try what you have said above. Unfortunately no issue is being created.

Nir Haimov
Community Champion
August 22, 2018

Hi @Khushbu Jowaheer

Do you still get an error?

If yes, what is the error? same one?

Khushbu Jowaheer August 22, 2018

Hi @Nir Haimov,

There is no failures reproduce . Please find the attached screenshot. It is the above code that I have used. 

Capture.PNG

 

Is there any possible solution you can suggest?

 

Thanks,

Khushbu

Nir Haimov
Community Champion
August 22, 2018

Hi @Khushbu Jowaheer

Please replace my above code with this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

Issue issue = issue
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def issueSummary = issue.summary
def issueDescription = issue.description
def issueReporter = issue.reporter

def issueFactory = ComponentAccessor.getIssueFactory()
def issueManager = ComponentAccessor.getIssueManager()
def newIssue = issueFactory.cloneIssue(issue)

newIssue.setIssueTypeId("3")
newIssue.setSummary(issueSummary)
newIssue.setDescription(issueSummary)
newIssue.setReporter(issueReporter)

Map<String,Object> newIssueParams = [issue:newIssue] as Map<String,Object>
issueManager.createIssueObject(currentUser, newIssueParams)

Let me know if it works 

Khushbu Jowaheer August 23, 2018

Hi Niv,

Nothing happens. I replaced the issue type id with the issue type id I want to create. I am trying to create issue of type sub task under the main issue.

Nir Haimov
Community Champion
August 23, 2018

Hi @Khushbu Jowaheer

The code i gave you should work.

It works for me, and create a regular task.

I wanted you try it just to see that it creates something for you.

Sounds weird for me that it does not create anything.

If you want to create sub-task, you need to use "issuetypeid" 5 (this is the issue type for sub-task in all instances coming by default with Jira)

You are trying different id and maybe the problem is there.

Furthermore, when creating sub-task you need to add another parameter to the code says who is the parent of the current sub-task you currently creating.

 

Any way,

Scriptrunner comes with a buildin script to create a sub-task (from post-function)

Use this will make your life easier i think

Khushbu Jowaheer August 23, 2018

Yes I was using the builtin script earlier but now I have 156 values in my multiselect custom field.

Instead of creating 156 post functions, I was trying to find a simpler way by writing a script.

If I select 3 options from the multiselect list, I want 3 sub tasks to be created.

Nir Haimov
Community Champion
August 23, 2018

I understand your case...

Well, i don't know what to tell you at this point.

My code should give you most of the way but you say it's not working for you  =/

Khushbu Jowaheer August 23, 2018

Hello Niv,

Actually you were right I need to specify the parentId in the code since I am creating a subtask, it is now working.

Thanks a lot :)

Nir Haimov
Community Champion
August 23, 2018

Great!

Please mark my answer as the solution so this case will be closed :)

Dante Labate December 19, 2018

@Khushbu Jowaheer

 

I would be very happy if you could share the script for creating subtasks.

0 votes
Sai Praveen Aminigadda February 12, 2021

@Nir Haimov i would like to use it to create stories from the creation of epic based on a custom field value. does this script work for my requirement?

Suggest an answer

Log in or Sign up to answer