Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,366,304
Community Members
 
Community Events
168
Community Groups

create mulitple subtasks from multiselect checkbox field

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

0 votes
Answer accepted

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

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"
}
}

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"])) 

That worked. Thanks for your help.

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events