Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner get values of subtask of a parent issue

Laura Jimenez Valencia August 14, 2023

Hi!

I want to create a script field in jira cloud called PX: Total Project Cost with scriptrunner.

This field collects the sum of the value of a field called PX: Cost of a ticket with the value of the PX: Change Request Cost field that contains the subtasks.

If the parent ticket has subtasks, I want the field script to loop through those subtasks, identify if any are of type Scope change, and retrieve the value of the PX: Change Request Cost field (which is inside the subtask).

Once you have collected the two fields (PX: Change Request Cost of the subtaks and PX: Cost of the parent, I want you to add their values.

I have this script that should work, but it doesn't access the subtasks so it never gets the value of the field inside them.

Currently the script just returns me the value of PX:Cost from the parent.

What am I doing wrong?

 

import groovy.json.JsonSlurper

// Obtener el número de ticket actual
def issueKey = issue.key
// IDs de los campos personalizados
def costFieldId = "customfield_10131" // ID del campo PX: Cost
def changeCostFieldId = "customfield_10126" // ID del campo PX: Change Request Cost
// Inicializar el total del costo
def totalCost = 0.0

// Obtener el valor del campo PX: Cost del problema actual
def resultCost = get("/rest/api/3/issue/${issueKey}?fields=${costFieldId}")
.header('Content-Type', 'application/json')
.asObject(Map)

// Extraer el valor del campo PX: Cost de la respuesta JSON
def costValue = resultCost.body.fields[costFieldId] as Double

// Agregar el valor de PX: Cost al total, si no es nulo
totalCost += costValue ?: 0.0

// Obtener las subtasks del problema actual
def subtasksResponse = get("/rest/api/3/issue/${issueKey}/subtask")
.header('Content-Type', 'application/json')
.asObject(Map)

// Extraer el contenido JSON de las subtasks
def subtasks = subtasksResponse.body
// Recorrer las subtasks del issue actual
subtasks.issues.each { subtask ->
// Verificar si la subtask es de tipo "Scope change"
if (subtask.fields.issuetype.name == "Scope change") {
def subtaskStatus = subtask.getStatus().getName();
if(subtaskStatus.equals("Approved") || subtaskStatus.equals("Resolved") || subtaskStatus.equals("Closed") ){
// Obtener el valor del campo PX: Change Request Cost de la subtask
def subtaskChangeCostValue = subtask.fields[changeCostFieldId] as Double
// Agregar el valor de PX: Change Request Cost al total, si no es nulo
totalCost += subtaskChangeCostValue ?: 0.0
}
}
}

return totalCost

1 answer

1 accepted

0 votes
Answer accepted
Lucy Russon _Adaptavist_ August 16, 2023

Hi Laura, 

Thanks for using ScriptRunner! Your script is only returning the value of PX:Cost from the parent because your subtask call isn't quite right. You need to get the subtasks field value from the parent issue and then use the Get Issue call for each subtask. I've provided a script below which does this, getting the cost value where the subtask type is "Scope Change" and the status is Done or In Progress.  


def issueKey = issue.key
def totalCost = 0.0
def status = ["Done", "In Progress"]

// Get parent issue
def result = get("/rest/api/2/issue/${issueKey}")
.header('Content-Type', 'application/json')
.asObject(Map)
if (result.status != 200) {
return "Error retrieving issue ${result}"
}
def fields = result.body.fields as Map

// Get the value of the subtask field on the parent issue
def subtasks = fields.subtasks as List<Map>

// Get each subtask issue
subtasks.forEach { Map subtask ->
def subtaskResult = get("/rest/api/2/issue/${subtask.id}")
.header('Content-Type', 'application/json')
.asObject(Map)
if (subtaskResult.status != 200) {
logger.error("Error retrieving subtask ${subtaskResult}")
}
def subtaskFields = subtask.fields as Map
def subtaskAllFields = subtaskResult.body.fields as Map
// Check the subtask type
if(subtaskFields.issuetype.name == "Scope Change"){
// Check the subtask staus against the status list above
if(status.indexOf(subtaskFields.status.name) > -1){
// Get the cost value and add to the totalCost
def subtaskCost = subtaskAllFields.customfield_10083 as Double
totalCost += subtaskCost
}
}
}

return totalCost

 I hope this helps! 

Laura Jimenez Valencia August 16, 2023

Thank you very much for your answer! I already solved it in another way, but it's good to see other ways to reach the same solution!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events