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 -> https://answers.atlassian.com/questions/29809/workflow-post-function-creating-subtask-for-each-user-of-group

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

This widget could not be displayed.
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
                        newIssue.setDueDate(issue.getDueDate())
						newIssue.setDescription(issue.getDescription())
 
                        Map<String,Object> newIssueParams = ["issue":newIssue] as Map<String,Object>
                        GenericValue newIssueGv = issueManager.createIssue(currentUserObj, newIssueParams)
 
                        boolean wasIndexing = ImportUtils.isIndexIssues();
                        ImportUtils.setIndexIssues(true);
 
                        indexManager.reIndex(newIssueGv);
 
                        SubTaskManager subTaskManager = componentManager.getSubTaskManager()
                        subTaskManager.createSubTaskIssueLink(issue, newIssue, componentManager.getJiraAuthenticationContext().getUser())
 
                        indexManager.reIndex(newIssueGv);
                        indexManager.reIndex(issue);
 
                        ImportUtils.setIndexIssues(wasIndexing);
 
                        UserList.add(user)
 
            }
}

This widget could not be displayed.
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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

181 views 2 0
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