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

Accepted Answer
0 votes
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 votes
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 Sign up to answer
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

23,579 views 2 7
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