Can't get Subtask Issue Object

Seth Bennett September 14, 2022

I have a Issue, which contains a field "Total Cost".

That issue has a series of SubTasks, each with a "Cost" field.

I want to collect the "Costs" of all these subtasks, total that value, and update that value to the "Total Cost" of their parent issue's custom field "Total Cost".

Ideally, I'd like to do this whenever one of these Issue Types is updated.

I've managed to gather a list of the SubTasks' ids, but when I apply the same code to open up those SubTasks, it fails. Here's my code:

***

import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField

IssueManager im = ComponentAccessor.getIssueManager()
MutableIssue issue = im.getIssueObject("PUB-239")

def IssueType = issue.issueType.name;
def OutPut = "";
MutableIssue subTaskDetails;

if(IssueType == 'Format Title'){

    // Get the issue's Total Cost Field
    def customFieldManager = ComponentAccessor.getCustomFieldManager();
    def cField = customFieldManager.getCustomFieldObjectsByName("Total Cost");
    def cFieldValue = issue.getCustomFieldValue(cField[0]);

    if(cFieldValue != "250"){

        def getArrayLength = issue.getSubTaskObjects().size();
        OutPut = "Array Length Check: "+getArrayLength;

        if(getArrayLength > 0){
            for(int i = 0; i<getArrayLength; i++) {
                def subTaskId = im.getIssueObject(issue.getSubTaskObjects()[i].toString());
               
                subTaskDetails = im.getIssueObject(subTaskId);
               
            }
        }
    }
}
return OutPut;
***

The last line: "subTaskDetails = im.getIssueObject(subTaskId);" Is triggering the following error: 

"[Static type checking] - Cannot find matching method com.atlassian.jira.issue.IssueManager#getIssueObject(com.atlassian.jira.issue.MutableIssue). Please check if the declared type is correct and if the method exists."

Apparently, Mutable Issues created by the IssueManager don't have access to the "getIssueObject()" method...

But I already used it successfully, earlier in the code!

Can anyone help me?

 

 

 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 14, 2022

The "getSubTaskObjects" already returns a list of Issue objects.

You don't need to extract the Id and retrieve the object again.

Try this:

import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField

IssueManager im = ComponentAccessor.getIssueManager()
MutableIssue issue = im.getIssueObject("PUB-239")

def issueType = issue.issueType.name;
def output = "";

if (issueType == 'Format Title') {
// Get the issue's Total Cost Field
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def totalCostCf = customFieldManager.getCustomFieldObjectsByName("Total Cost");
def costCf = customFieldManager.getCustomFieldObjectsByName("Cost");
def currentTotalCost = issue.getCustomFieldValue(totalCostCf[0]);

if (currentTotalCost != "250") {
def subtaskCount = issue.subTaskObjects.size()
output = "Count of subtasks: $subtaskCount"
def newTotalCosts = issue.subTaskObjects.sum { subtask ->
subtask.getCustomFieldValue(costCf) as Number ?: 0
}
output = "$output<br>Sum of costs from subtaks: $newTotalCosts"
}
}
output

Now, if you want to have this automated, my recommendation would be to create a Custom Listener. Listen for Issue Updated and Issue Created events. 

And the script would look something like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def subTaskIssueName = 'Subtask'
def parentIssueTypeName = "Format Title"
def sendMailOnParentUpdate = true

def issue = event.issue

if(!issue.subTask) return null //not a subtask, nothing to do
if(issue.issueType.name != subTaskIssueName) return null //not the correct type of subtask, nothing to do
if(issue.parentObject.issueType.name != parentIssueTypeName ) return null //the parent is not correct type, nothing to do

//all validations passed, find all the subtasks and update the parent
def parentIssue = issue.parentObject

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def totalCostCf = customFieldManager.getCustomFieldObjectsByName("Total Cost")[0]
def costCf = customFieldManager.getCustomFieldObjectsByName("Cost")[0]

def newTotalCost = parentIssue.subTaskObjects.findAll{it.issueType.name == subTaskIssueName}.sum { subTask ->
subTask.getCustomFieldValue(costCf) as Number ?: 0
}

def currentTotalCost = parentIssue.getCustomFieldValue(totalCostCf)
if(currentTotalCost != newTotalCost){
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueService = ComponentAccessor.issueService
def iip = issueService.newIssueInputParameters()
iip.addCustomFieldValue(totalCostCf.id, newTotalCost.toString())

def validationResult = issueService.validateUpdate(currentUser, parentIssue.id, iip)
if(!validationResult.valid){
log.error "Issue Update validation failed for $parentIssue.key when attempting to update $totalCostCf to $newTotalCost: $validationResult.errorCollection"
return null
}
def updateResult = issueService.update(currentUser, validationResult, EventDispatchOption.ISSUE_UPDATED, sendMailOnParentUpdate)
if(!updateResult.valid){
log.error "Issue Update failed for $parentIssue.key when attempting to update $totalCostCf to $newTotalCost: $updateResult.errorCollection"
return null
}
log.info "Successfully updated $totalCostCf for $parentIssue.key to $newTotalCost"
} else {
log.debug "Nothing to update, the $totalCostCf already has the correct value."
}

This script will be fired whenever an issue is updated in your project.

But it will exit early if 1) it's not a subtask, 2)it's not the correct type of subtask, or 3) the parent is not the correct type.

Then it will proceed to calculate the sum of costs and trigger an update transaction on the parent issue.

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 14, 2022

I should mention that I've not validated this with Jira 9.

I'm still on Jira 8.13. But I don't expect any issues.

Seth Bennett September 19, 2022

Thanks, I appreciate the input :)

TAGS
AUG Leaders

Atlassian Community Events