workflow post-function: creating subtask for each user in multi user picker CF

Hi all,

I need a workflow post-function which creates a subtask for each user in "multi user picker" custom field and assign it to him/her.

I found a similar topic about this ->

In this code, subtask is created for each user of a group but I have a multi user picker" custom field so I need to modify the code but I couldn't. Any help would be greatly appreciated.

2 answers

1 accepted

The code below worked for me. Thanks.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.config.SubTaskManager
import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.user.UserUtils
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.util.UserManager
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.fields.CustomField
ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
def issueFactory = componentManager.getIssueFactory()
def issueManager = componentManager.getIssueManager()
def indexManager = componentManager.getIndexManager()
CustomField yetkilicf = customFieldManager.getCustomFieldObject(11421L) // "yetkilicf" is the name of my multi user picker cf
Collection<User> users=  (Collection) issue.getCustomFieldValue(yetkilicf)
def UserList = []

String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller()
User currentUserObj = UserUtils.getUser(currentUser)
for (User user : users) {
                if(!UserList.contains(user) && user != currentUser) {
                        MutableIssue newIssue = issueFactory.getIssue()
                        newIssue.summary = issue.summary
                        newIssue.issueTypeId = '35'
                        newIssue.project = issue.project
                        newIssue.reporter = UserUtils.getUser(currentUser)
                        newIssue.assignee =  UserUtils.getUser(user)
                        newIssue.assigneeId = user.getName()
                        newIssue.priority = issue.priority
                        Map<String,Object> newIssueParams = ["issue":newIssue] as Map<String,Object>
                        GenericValue newIssueGv = issueManager.createIssue(currentUserObj, newIssueParams)
                        boolean wasIndexing = ImportUtils.isIndexIssues();
                        SubTaskManager subTaskManager = componentManager.getSubTaskManager()
                        subTaskManager.createSubTaskIssueLink(issue, newIssue, componentManager.getJiraAuthenticationContext().getUser())

0 vote
Henning Tietgens Community Champion Nov 21, 2013

You should use the subtask creation code from the topic you referenced above and run this in a loop like this

cfValues['My Multiuser Picker'].each{ user ->
    // create subtask for user

'My Multiuser Picker' is the name of the multi user picker custom field.

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,301 views 14 20
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot