I'm trying to fill a custom field with the total amount of work spent on the issue and it's subtaks

Using Scriptrunner add-on in Jira Cloud, I'm trying to fill a custom field with the total amount of work spent on this ticket and all of it's subtasks. I added the Scriptrunner add-on to my cloud instance and tried to write some code using the examples on http://scriptrunner-docs.connect.adaptavist.com/jiracloud/script-listeners.html.

I inserted my code as a Script Listener that responds to the Issue Created and Issue Updated events. I think I can't use Worklog * because of the Tempo Timesheets add-on.

The code I have now generates the following logs, but I think there are more things wrong with this code.

2017-04-26 14:18:13.128 INFO - Serializing object into 'interface java.util.List'
2017-04-26 14:18:13.139 INFO - GET /rest/api/2/issue/25909/worklog asObject Request Duration: 205ms
2017-04-26 14:18:13.152 ERROR - Cannot get property 'body' on null object on line 38
2017-04-26 14:18:13.164 ERROR - Class: com.adaptavist.sr.cloud.events.ScriptExecution, Config: null

This is the code I have now:

setTotalTimeSpent(issue.id, !issue.fields.issuetype.subtask)

if (issue.fields.issuetype.subtask) {
    setTotalTimeSpent(issue.fields.parent.id, Boolean.TRUE)

void setTotalTimeSpent(String issueId, Boolean includeSubtasks) {
    def totalTimeSpent = getTotalTimeSpent(issueId)
    if (includeSubtasks) {
        def allSubtasks = get("/rest/api/2/search")
            .queryString("jql", "parent=${issueId}")
            .queryString("fields", "[]")
            .issues as List<Map>
        totalTimeSpent += allSubtasks.collect { Map subtask -> 
    def response = put("/rest/api/2/issue/${issueId}")
        .queryString("overrideScreenSecurity", Boolean.TRUE)
        .header("Content-Type", "application/json")
                "customfield_10701": totalTimeSpent
    assert response.status >= 200 && response.status < 300

float getTotalTimeSpent(String issueId) {
    def worklogs = get("/rest/api/2/issue/${issueId}/worklog")
        .worklogs as List<Map>

    return worklogs.collect { Map worklog -> worklog.timeSpentSeconds / 3600.0 }.sum()

If anyone has more experience with Scriptrunner, please help me to get working code.

1 answer

0 votes
Alex Christensen Community Champion Apr 26, 2017

I'm not sure that you need a custom field to capture this information. In the Time Tracking section on an issue with sub-tasks, there should be a checkbox that says, "Include sub-tasks."


The "Logged" bar shows all worklogs for the parent issue and its subtasks. You can also view this information in the Issue Navigator using the Σ Time Spent column.

We used this solution when we used Jira Server, but we migrated to Jira Cloud and there I can't find the checkbox. 

The Σ Time Spent field only shows all time I logged on this issue, not the time my teammates logged.

Thank you for your response you pointed me in the right direction. The Tempo Timesheets plugin removes this checkbox and breaks the Σ Time Spent field. I wil report a bug with them to get this fixed.

Not a resolution to my question so I'm not marking it as answered.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Jan 08, 2019 in Jira

How to Jira for designers

I’m a designer on the Jira team. For a long time, I’ve fielded questions from other designers about how they should be using Jira Software with their design team. I’ve also heard feedback from other ...

1,173 views 5 10
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you