Hello,
I have an issue that I am struggling to understand why its happening. I have created a Scriptrunner Listener which listens out for when an issue is updated, created or deleted. I first have a check to make sure I only deal with a particular issue type of interest (a sub-task).
The script gets the parent of the sub-task which triggered the event and then gets all subtasks, loops through each one, extracts a property and adds that to a list field in the parent.
Problem: The issue I am facing is that when I call getSubTaskObjects() I seemingly get back all sub-tasks except for the sub-task that was just created.... even though this exists as it triggered the creation event. I cannot for the life of me figure out what is going on.
Any help in shedding some light on my issue is greatly appreciated
This is the script I have;
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
// Custom field identifiers
final String TEAM = "customfield_14712";
final String DELIVERY_TEAMS = "customfield_14711";
final String OUTCOME = "customfield_14708";
MutableIssue triggerissue = (MutableIssue)event.getIssue()
// Apply logic only to the Deliverable issue type
if (triggerissue.getIssueType().name == "Deliverable") {
// Initialise variables / objects
MutableIssue parentIssue = (MutableIssue)triggerissue.parentObject
List<Issue> childIssues = (List)parentIssue.getSubTaskObjects()
log.warn(childIssues.size())
//ALWAYS IS ONE LESS THAN WHAT IT SHOULD BE
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomField cfTeam = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(TEAM)
log.warn("new Delivarble team: "+triggerissue.getCustomFieldValue(cfTeam))
//THIS IS TO TEST I CAN ACCESS THE TRIGGER ISSUE DIRECTLY, WHICH I CAN
CustomField cfOutcome = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(OUTCOME)
CustomField cfDeliveryTeams = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(DELIVERY_TEAMS)
List teamsToAdd = new ArrayList() //Where we will load the Delivery Team property into for each sub-task
def parentOutcome = parentIssue.getCustomFieldValue(cfOutcome)
// If the custom fields do not exist, exit
if (!cfTeam || !cfDeliveryTeams || !cfOutcome) {
log.warn("No custom field found in Scriptrunner Listener for Delivery Team changes")
return
}
// Cycle through each sub-task to get the Delivery Team property value and add to a List
childIssues.each {
def radioValue = it.getCustomFieldValue(cfTeam)
log.warn(radioValue)
if (radioValue != null && !teamsToAdd.contains(radioValue)) {
teamsToAdd.add(radioValue)
}
}
parentIssue.setCustomFieldValue(cfDeliveryTeams, teamsToAdd) // Update Delivery Team(s) custom field with values gathered from the sub-tasks
issueManager.updateIssue(user, parentIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
log.warn("Updating Idea ($parentIssue.key Delivery Team(s) with $teamsToAdd")
triggerissue.setCustomFieldValue(cfOutcome, parentOutcome) //Update the Delverable with the Outcome from the parent Idea
issueManager.updateIssue(user, triggerissue, EventDispatchOption.DO_NOT_DISPATCH, false)
log.warn("Updating Deliverable ($triggerissue.key) Outcome with $parentOutcome")
//Re-index the sub-task after update
boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
ComponentAccessor.getComponent(IssueIndexingService.class).reIndex(triggerissue)
ComponentAccessor.getComponent(IssueIndexingService.class).reIndex(parentIssue)
ImportUtils.setIndexIssues(wasIndexing)
} else {
// The event was not on a Deliverable ticket, so exit
log.warn("This issue type is not a Deliverable")
return
}