Scripted Post-Function to create a subtask for each member of selected group

Kassidy_Kenney March 3, 2017

Workflow:

  1. User creates Task type: Peer Review Task
    1. Picks a "Peer Review Group" from Single Group Picker on issue form
  2. User clicks "Send Peer Review Requests"
    1. Post-Function: subtasks are created (and assigned) for each member of group (selected in original task)

My script is really close. When I hard-code in a user group, everything works. My problem is when I pull the group from the Peer Review Task, I am getting "[com.atlassian.crowd.embedded.impl.ImmutableGroup@6e019bb3]" instead of the group name "TCR_Admin".

I am using JIRA Server version 6.3.15 and ScriptRunner version 4.1.3.9

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.crowd.embedded.api.Group
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.crowd.embedded.api.User
Issue parentIssue = issue
//Dont run this script on a subtask
if (parentIssue.isSubTask())
return
//log behaviors against current user
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()

//Get the group name from the original issue
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def groupfield = customFieldManager.getCustomFieldObjectByName("Peer Review Group")
def groupname = parentIssue.getCustomFieldValue(groupfield) as String

//def groupname = "TCR_Admin" //>> Manually setting the group makes the script work
//parentIssue.summary = groupname as String //>>The group name displays as "[com.atlassian.crowd.embedded.impl.ImmutableGroup@6e019bb3]"
//Find how many users in the group (equals how many subtasks to make)
def groupManager = ComponentAccessor.getGroupManager()
def GroupSize = groupManager.getUsersInGroup(groupname).size()
int max = GroupSize
//Set up to create and link new issues
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def constantManager = ComponentAccessor.getConstantsManager()
//Create a subtask and assignee a subtask for each member of the group
def assignee;
int i;
for (i = 0; i < max; i++) {
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setSummary("subtask summary")
assignee = groupManager.getUsersInGroup(groupname).get(i).getName() //Throws a typecheck error but works okay
newSubTask.setAssigneeId(assignee) 
newSubTask.setParentObject(parentIssue)
newSubTask.setProjectObject(parentIssue.getProjectObject())
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{
it.getName() == "Sub-task"
}.id)
// Add any other fields you want for the newly created sub task

Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
issueManager.createIssueObject(user.directoryUser, newIssueParams)
subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user.directoryUser)
}

2 answers

1 accepted

2 votes
Answer accepted
Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 3, 2017

Hi Kassidy 

Try something like 

def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("GroupPicker")

// either a single or multi group picker it always return a list of group/s
def group = issue.getCustomFieldValue(cf)
 
// if there is actually a group het it's name
def groupName = group ? group[0].name : null

Please let me know if this does the trick.

regards, Thanos

Kassidy_Kenney March 3, 2017

Thanos, 

Thanks for the quick reply! That worked!

Kassidy

Tukaram Bhukya February 13, 2018

Please could someone paste here updated working script for jira version 7.X

Deleted user July 9, 2019

@Thanos Batagiannis _Adaptavist_ This looks very exciting. I'm eager to try this but in my case I'm trying to have the script create subtasks for each user listed in a custom (user-picker) field.  Do you have a suggestion for replacing the references to "group" with perhaps the user from the custom field value?

def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("UserPicker")

// either a single or multi USER picker it always return a list of user/s
def group = issue.getCustomFieldValue(cf)
 
// if there is actually a user get that user from the custom field of the parent
NOT SURE What to do here???

 

2 votes
Steve Van Der Weide May 4, 2017

A note for anyone using this script in the future: 

If you are using JIRA v.7 or later, you will need to change the two instances of "user.directoryUser" to "user".  

Ankit Patel April 6, 2018

Steve, I am trying to display the Group name in the custom email using Scriptrunner, here is the code I am using but still get the Immutablegroup in response: 

${issue.getCustomFieldValue(com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Assigned Group"))}

 

Any help would be much appreciated

 

Thanks
Ankit

Suggest an answer

Log in or Sign up to answer