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

0 votes
Accepted answer
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)
 
            }
}

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
Published Jan 08, 2019 in Jira

How to Jira for designers

I’m a designer on the Jira team. For a long time, I’ve fielded questions from other designers about how they should be using Jira Software with their design team. I’ve also heard feedback from other ...

1,100 views 4 9
Read article

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