Forums

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

Create multiple issues based on the values on a custom field

João Silva Corrêa
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 28, 2018

Hi guys,

I'm trying to create an issue and, according to a certain multi selection custom field (suppliers) I'd like to raise multiple issues of another issuetype. I mean:

Example:

Create issue (user story)

If supplier = "A" and "B"

Create issue (bug) for A

Create issue (bug) for B

Until now I am able to create them without any problem but as soon as I'd like to get the current value of the supplier, scriptrunner copies all the values instead of the current one.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.issue.fields.NavigableField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants

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_10201")
String selectedSuppliers = issue.getCustomFieldValue(suppliers)
def issueSummary = issue.summary
def issueDescription = issue.description
def issueReporter = issue.reporter

selectedSuppliers?.each {LazyLoadedOption it ->;
def issueFactory = ComponentAccessor.getIssueFactory()
def issueManager = ComponentAccessor.getIssueManager()
def newIssue = issueFactory.cloneIssue(issue)

newIssue.setIssueTypeId("10101")
newIssue.setSummary(issueSummary)
newIssue.setDescription(issueSummary)
newIssue.setReporter(issueReporter)
newIssue.setCustomFieldValue(suppliers, selectedSuppliers)

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

How can I point 

 newIssue.setCustomFieldValue(suppliers, selectedSuppliers)

to 

selectedSuppliers?.each

?

 

More over, is it possible to link the newly created issues to the trigger one?

 

Cheers!

1 answer

1 vote
Jenna Davis
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.
February 28, 2018

selectedSuppliers is a list of all of the selected options from your multiselect, you need to use the 'it' variable that you get from your .each loop to assign a single value from this list to your new issue. 

Try out this code, I did not directly test it but I made a few changes that I 'think' will fix the problems you mentioned:

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_10201")
def selectedSuppliers = issue.getCustomFieldValue(suppliers)
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("10101")
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)
}

 If I misunderstood your issue or if you have any other questions, please let me know. :)

Jenna

João Silva Corrêa
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 5, 2018

Hi Jenna,

 

Thanks for your answer, it is working now! :)

Moreover, do you have any clue on how could I set the security level based on this custom field value?

Suggest an answer

Log in or Sign up to answer