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