create mulitple subtasks from multiselect checkbox field

Kieran_Donnelly March 1, 2018

My issue create screen has a multi-select checkbox custom field. I would like a scriptrunner post-function that will:

a) create a sub-task for each option selected

b) use the name of the selected checkbox in the sub-task summary

 

Example:

My custom field is called: "colors"

The following options exist:

  • "blue"
  • "green" -> checked
  • "red" -> checked

 

Desired outcome from post-function: Create two sub-tasks with summary as follows:

1) "Subtask - green"

2) "Subtask - red"

I am using jira server 7.x and latest scriptrunner plugin.

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Alexey Matveev
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 1, 2018

I think you should try to write a script yourself first. You can find info on how to create subtasks using scriptrunner here:

https://community.atlassian.com/t5/Jira-questions/How-to-Automatically-create-Sub-task-using-script-runner-post/qaq-p/400564

Kieran_Donnelly March 5, 2018

I created the following script which works.

However I have not figured out how to set a checkbox in the newly created sub-task as indicated in the code comment.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.customfields.manager.OptionsManager

import com.atlassian.jira.user.ApplicationUser

import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

def constantManager = ComponentAccessor.getConstantsManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def issueManager = ComponentAccessor.getIssueManager()
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
Issue parentIssue = issue

def customFieldManager = ComponentAccessor.customFieldManager
def cf = customFieldManager.getCustomFieldObject("customfield_10107")
def summariesList = (issue.getCustomFieldValue(cf) as Collection<Option>)*.value

summariesList.each {
if (!issue.getSubTaskObjects()*.summary.contains(it)) {
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setAssigneeId(parentIssue.assigneeId)
newSubTask.setSummary(it)
newSubTask.setParentObject(parentIssue)
newSubTask.setProjectObject(parentIssue.getProjectObject())
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{it.getName() == "Sub-task"}.id)

// I want to set a checkbox field in newly created sub task next


def optionsManager = ComponentAccessor.getComponent(OptionsManager)
def cFM = ComponentAccessor.getCustomFieldManager()
def cff = customFieldManager.getCustomFieldObject("customfield_10107")
def fieldConfig = cff.getRelevantConfig(issue)
def option = optionsManager.getOptions(fieldConfig).getOptionForValue("${it.toString()}", null)
newSubTask.setCustomFieldValue(cf, [option])

def newIssueParams = ["issue" : newSubTask] as Map<String,Object>
issueManager.createIssueObject(user, newIssueParams)
subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user)

log.info "Issue with summary ${newSubTask.summary} created"
}
}
Alexey Matveev
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 5, 2018

Kindly read my article here

https://community.atlassian.com/t5/Agile-articles/Three-ways-to-update-an-issue-in-Jira-Java-Api/ba-p/736585

You use the frist method in my article. You should get options for the field like this

def List<Option> getOptions(Issue issue, CustomField customField, List<String> optionList) {
    def config = customField.getRelevantConfig(issue)
    def options = ComponentAccessor.getOptionsManager().getOptions(config)
    def optionsToSelect = options.findAll { it.value in optionList }
}

and set the value like this

issue.setCustomFieldValue(checkbox_field, getOptions(issue, checkbox_field, ["option 1", "option 2"])) 
Kieran_Donnelly March 6, 2018

That worked. Thanks for your help.

TAGS
AUG Leaders

Atlassian Community Events