Autofill field "time spent" when issue status is changed
Asked by Kirill Gutmann:

I am trying to track how much time issue spends in status "in progress" and transitions to "Done" by automatically updating "time spent".

Idea is to have "time spent" start tracking as soon as issue will leave "To do" to "In Progress" and finishes tracking as soon as status changes to "Done" and automatically updates the field "time spent".

Keeping in mind that it syncs with before applied 5 days and 8 hours of working regime in Jira.

Is it possible ?

We are using jira standard cloud version and jira classical software project.


Answers:
author: Ravi Sagar _Sparxsys_
body: Hi [@Kirill Gutmann](/t5/user/viewprofilepage/user-id/4206188)

Doing something like this is quite easy when you use scripting (both on server or cloud). This information is stored in the change history on an issue which can be fetched both by Jira Java API as well as REST API.

In case you are on server and have ScriptRunner then this [script](https://library.adaptavist.com/entity/times-an-issue-was-in-each-status) will help you in getting the change history like when the status changed?

In case you are on cloud (which is my guess) then using either ScriptRunner for Jira on Cloud or the built in Automation you can call Jira REST API to fetch the change history. I would prefer using ScriptRunner as writing a groovy script is easier, a bit more readable and can handle complex cases. In both cases you need this end point.

```
JIRAURL/rest/api/3/issue/{{issue.key}}?expand=changelog
```

Let us say you want to do it using Automation rules, then when you call this end point. You will get a response JSON back in a smart value which you can access like this.

```
{{webhookResponse.body.changelog.histories}}
```

Now if you log this smart value you will get the json of the histories part of all the change histories. Now you can further iterate over those list of items in the change histories.

```
{{#webhookResponse.body.changelog.histories}}
ID: {{id}}
Created: {{created}}
{{#items}}{{fromString}} - {{toString}} {{/}}
{{/}}?
```

This will give you all the items in the change log like this.

```
ID: 18206
Created: 2021-01-02T17:11:59.730+0000
To Do - In Progress?

ID: 18152
Created: 2021-01-01T16:40:30.859+0000
To Do - In Review
```

Going back to your requirement, once you know how to fetch a change history, then you can perform some calculations on the [dates](https://support.atlassian.com/jira-software-cloud/docs/use-smart-values-to-manipulate-and-format-dates/) for specific field changes.

The second part of your requirement was to log work. There is an action to do that or you can do that using REST API.

I hope this will help you, it is a bit complex but lot of fun :)

Ravi

comments: author: Ravi Sagar _Sparxsys_
body: ..btw you can't auto fill a field but can log work using automation.


author: Kirill Gutmann
body: Thank you very much, this is very helpful!


author: Ansar Rezaei
body: Hi [@Ravi Sagar _Sparxsys_](/t5/user/viewprofilepage/user-id/853596)   
Thanks for your great comment.  
Could you help me how can I get the fromString value for the issue key?  
I tried many things and couldn't get it.

```
{{#webhookResponse.body.changelog.histories.items.key}}

{{fromString}}

{{/}}
```

```
?{{#webhookResponse.body.changelog.histories.items.field.key.fromString}}{{/}}?
```

```
{{#webhookResponse.body.changelog.histories.items.field}}{{/}} 
```


author: Bill Sheboy
body: Hi [@Kirill Gutmann](/t5/user/viewprofilepage/user-id/4206188)

Adding onto what [@Nic Brough -Adaptavist-](/t5/user/viewprofilepage/user-id/781454) suggests for trying this with automation, you may want to add a couple of custom fields to make this easier, and then increment time spent based on those. For example...

* Add custom fields for Last Status Category, Last Status Category Changed
* Add a rule, triggered on issue create, which sets the custom fields. Note that {{issue.statuscategorychangedate}} is a text field so consider converting it with toDate.
* Add a rule, triggered on issue transition, to check for any changes to Status Category (compared to Last Status Category). When it changes, do the math needed to increment time spent. And then update the custom fields again, as needed.

Best regards,

Bill


author: Mariana_Pryshliak_Saasjet
body: Hello [@Kirill Gutmann](/t5/user/viewprofilepage/user-id/4206188)

As an alternative solution, you can try [Time between statuses](https://marketplace.atlassian.com/apps/1221462/time-between-statuses?hosting=cloud&utm_source=Atlassian_Community&utm_medium=Comments&utm_campaign=TBS_cloud) by SaaSJet, which is an Atlassian marketplace vendor.

![image.jfif](https://community.atlassian.com/t5/image/serverpage/image-id/125802iCDD239B292F4D315/image-size/large?v=v2&px=999 "image.jfif")

This add-on tracks transition time between statuses (e.g. from "in progress" to "done"). It also helps to set such calculation conditions as first/last transition and to/from status.

I hope, you find it helpful

Best regards, Mariana


author: Mehmet A _Bloompeak_
body: Hi [@Kirill Gutmann](/t5/user/viewprofilepage/user-id/4206188) ,

As an alternative, you can try [Status Time app](https://marketplace.atlassian.com/apps/1221826/status-time?hosting=server&tab=overview) developed by our team. It provides reports on how much time spent in each status as well as status entry dates and status transition count. Report can be exported as CSV, so that you can use external tools like Microsoft Excel or Google Spreadsheet to process the exported data.

Once you enter your working calendar into the app, it takes your working schedule into account too. That is, "In Progress" time of an issue opened on Friday at 5 PM and closed on Monday at 9 AM, will be a few hours rather than 3 days. It has various other reports like assignee time, status entry dates, average/sum reports(eg. average in progress time per project).

Here is the [online demo link](https://status-time.bloompeak.io/demo), you can see it in action and try.

<br />

If you are looking for a **free** solution, you can try the limited version [Status Time Free](https://marketplace.atlassian.com/apps/1222051/status-time-free).


author: Nic Brough -Adaptavist-
body: You'll need one of the scripting or automation apps to do this, and it won't work quite the way your describe.

Cloud does not support "pre-fill" properly, there's no way for an app to inject the required code. The best you can do is pre-calculate and fill in a custom field during an action such as edit or transition.

comments: author: Kirill Gutmann
body: Pre-calculating you mean, estimating on our own and posting our estimation to "time spent" ?

Approx. like this:

Auto time spent:

{

"update": {

"worklog" : \[

{

"add": {

"timeSpent" : "6m"

}

}

\]

},

"fields": {

"timetracking": {

"originalEstimate": "10",

"remainingEstimate": "5"

}

}

}


author: Kirill Gutmann
body: Thank you for your reply, understood.