Forums

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

Help with script on scriptrunner!

Laura Jimenez Valencia August 23, 2023

Hi!

I'm testing a script with the scriptconsole that does the following:

Check if an epic has children.

Go into those children and see if they each have a numeric field, if they do, the values ​​are added.

Then check if those children of the epic have subtasks, if those subtasks have the aforementioned numeric field, it also takes their values ​​and adds them.

Once this is done, it takes the sum of the values ​​of the fields of the children and the sum of the values ​​of the fields of the subtasks and adds them all.

The problem is that it is not taking the value of the subtasks field (it appears null), and this field exists within the subtasks and has a numerical value.

I don't understand what I'm doing wrong or if I'm not accessing the fields of the subtasks. The fields that should appear do not appear in the JSON response.

I paste the code here:

// Obtener el número de ticket actual
def epicKey = "IN2CORP-1884"

def issueCostField = "customfield_10240" // ID del campo PX: Cost
//def subtaskCostField = "customfield_10240" // ID del campo PX: Change Request Cost

def issuesSum = 0.0

// Construir la URL de la consulta JQL
def jql = '/rest/api/2/search?jql="Epic Link"=' + epicKey
jql = jql.replace(" ", "%20").replace('"', "%22")

// Realizar la consulta a la API de Jira
def epicLinkResponse = get(jql).asObject(Map)
def epicIssues = epicLinkResponse.body.issues

// Inicializar la variable subtaskSum fuera del bucle principal
def subtaskSum = 0.0

// Iterar a través de las issues vinculadas a la épica
epicIssues.each { issue ->
def issueKey = issue.key
def issueCost = issue.fields[issueCostField] as Double

{{if (issueCost != null) {
logger.info("Cost value for ${issueKey}: ${issueCost}")
issuesSum += issueCost
logger.info("Sum of issues cost: ${issuesSum}")
} else {
logger.info("Issue ${issueKey} has no valid cost.")
}

// Acceder a las subtasks de las issues
def subtasks = issue.fields.subtasks

// Iterar a través de las subtareas
subtasks.each { subtask ->
def subtaskKey = subtask.key
logger.info("subtask key: ${subtaskKey}") // mensaje de depuración
// def subtaskCost = subtask.fields[subtaskCostField] as Double
def issueCost1 = subtask.fields[issueCostField] as Double
logger.info("issueCost1: ${issueCost1}") // mensaje de depuración
if (subtask.fields.issuetype.name == "Sub-task(EN)") {
logger.info("Subtasks for ${issueKey} with Scope change issuetype: ${subtaskKey}") // mensaje de depuración
logger.info("issueCost: ${issueCost1}") // mensaje de depuración
if (issueCost1 != null) {
logger.info("issueCost1: ${issueCost1}") //mensaje de depuración
subtaskSum += issueCost1 ?: 0.0
}else {
logger.info("Issue ${subtaskKey} has no valid issueCost1.")
}
}
}}}

}

// Imprimir el total acumulado de los costos de las subtareas
logger.info("Total subtask cost: ${subtaskSum}")
return issuesSum + subtaskSum

The log is the following:

Serializing object into 'interface java.util.Map'
GET /rest/api/2/search?jql=%22Epic%20Link%22=IN2CORP-1884 asObject Request Duration: 661ms
Cost value for IN2CORP-1886: 10.0
Sum of issues cost: 10.0
Cost value for IN2CORP-1885: 10.0
Sum of issues cost: 20.0
subtask key: IN2CORP-1887
issueCost1: null
Total subtask cost: 0.0

The issueCost1 must be: 30

And the total subtask cost must be: 30

The return is 20, but it must be 50.

1 answer

1 accepted

0 votes
Answer accepted
meliodas16
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.
August 23, 2023

Hello @Laura Jimenez Valencia ,

You are not getting the expected result because the response you're parssing doesn't contain the "issueCostField" as part of "issue.fields.subtasks" and that's because you don't get all the fields of the subtask(s) returned as part of the response you get from "/rest/api/2/search?jql=".

If you check your JSON, you'll notice that customfield_10240 is not present in the subtasks part.

You have to retrieve the actual subtask object (for example via get(subtask.self).asObject(Map).body) to be able to access all fields related to the subtask.

Hope this help !

Kind regards.

Laura Jimenez Valencia August 28, 2023

Thank you so much @meliodas16 ! I did what you said and it worked perfect! I was able to get the fields that I was missing!

Suggest an answer

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

Atlassian Community Events