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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.