It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to create subtask depending on Select List (multiple choices)

How to create subtask depending on Select List (multiple choices)

2 answers

Hello @Mobily Monks 

Can you please clarify the question?

What actually do you want to do? Automatically create subtasks depending on the select list values? If yes, when do you want to create? In create transition's post function?

Are you looking for a solution via code (e.g. Script runner) or do you want to achieve that with Jira and/or plugins?

Best

Yes, we have a set of subtasks which we would like them to be automatically created when select list values are selected.

I am looking for a code( Script runner) solution and NOT apparently with plugins.

I tried to write the script as below. I did not test it, I just wrote here in the answer section.

But, it might be a good guide to start. 

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def issueManager = ComponentAccessor.getIssueManager()
def constantManager = ComponentAccessor.getConstantsManager()

CustomField customField = customFieldManager.getCustomFieldObject("customfield_10xxx")
List<Option> options = issue.getCustomFieldValue(customField)


options.each {
// these are the selected options, you can use these option values in this loop
def optionValue = it.value
def optionId = it.optionId
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setAssigneeId(issue.assigneeId)
newSubTask.setSummary("sub task summary")
newSubTask.setParentObject(issue)
newSubTask.setProjectObject(issue.getProjectObject())
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{
it.getName() == "Sub-task"
}.id) // or just give the issueTypeId for sub-task

// set any custom field if you want
newSubTask.setCustomFieldValue(cf, value)
def newIssueParams = ["issue" : newSubTask] as Map<String,Object>

issueManager.createIssueObject(authorUser, newIssueParams)
subTaskManager.createSubTaskIssueLink(issue, newSubTask, authorUser)

log.info "Issue with summary ${newSubTask.summary} created"
}

Let me know if you have any issues

Cheers

Tuncay Senturk

I have to update customfield_10xxx with the actual value 10406 and this code is good to go. Please confirm.

Or Do I have to update  customField  with the  custom Field Name  and options with any value? 

 

CustomField customField = customFieldManager.getCustomFieldObject("customfield_10xxx")
List<Option> options = issue.getCustomFieldValue(customField)

Yes, of course you need to change 10xxx with your custromfield id, like: customfield_10406. 

Hi again @Mobily Monks 

Have you achieved? If it helped, please accept the answer so that other community users might see the solution.

Thanks

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Jira

The add-in you’ve been waiting for: Jira Cloud for Excel 🙌

Introducing Jira Cloud for Excel Here at the product integrations team at Atlassian, we are thrilled to announce the new Jira Cloud for Excel add-in! This add-in lets you export Jira data directly ...

958 views 8 25
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you