Jira Automation to Check Time in Story and Subtask before Deleting
Asked by Jason Krewson:

We normally allow project leads and certain users the ability to delete issues in Jira but recently we have had an outbreak of users deleting issues with time logged to them and that is a big issue so in the short term I have disabled the ability to delete cards for everyone.

Long term possible solution is:

* No one has the ability to delete.
* We introduces a new status, To be Deleted, we allow project leads to move cards to this status.
* When a card is moved to the To be Deleted Status automation runs a check on it and actions it depending on what it finds (picture below of the automation).
  * If it has timespent automation moves the card back to In Progress and emails the assignee letting them know that the card has timespent on it so we are unable to delete, contact the Jira team if you need more help.
  * If it has no timespent the automation deletes it.

![Screenshot 2024-08-30 132401.png](/t5/image/serverpage/image-id/344858i0C88DA6B4FF7FB0F/image-size/large?v=v2&px=999 "Screenshot 2024-08-30 132401.png")

The issue is, what if this is a Story and the Subtask has time?

So first thought was just use sigma timespent and that solves the issue, but after checking the automation possibilities and researching online I am finding that isn't possible.

I also tried to have it check the subtask for matches with time, but this also doesn't seem possible.

**So the question would be, is there a way to have this automation also verify the Subtask doesn't have time as well before deleting?**


Answers:
author: Trudy Claspill
body: Hello [@Jason Krewson](/t5/user/viewprofilepage/user-id/5437316)

Yes!

After the trigger add a Lookup Issue action to search for subtasks under the trigger issue where the subtask has Timespent

![Screenshot 2024-08-30 at 1.09.30?PM.png](https://community.atlassian.com/t5/image/serverpage/image-id/344870iE301770523AFD084/image-size/large?v=v2&px=999 "Screenshot 2024-08-30 at 1.09.30?PM.png")

(If this is for a Team managed project, use **Subtask** instead of **Sub-task**.)

Then in your IF-Else block in the first IF where you are checking for Timespent on the issue, change the condition set to be "Any of" and add this smart value condition

![Screenshot 2024-08-30 at 1.12.32?PM.png](https://community.atlassian.com/t5/image/serverpage/image-id/344872i553D2CB2D2774C12/image-size/large?v=v2&px=999 "Screenshot 2024-08-30 at 1.12.32?PM.png")

If any subtasks with Timespent are found, then the size of of result set in Lookup Issues will be greater than 0.

So if either a subtask has Timespent or the main issue has Timespent, transition the main issue back to In Progress.

comments: author: Jason Krewson
body: Thanks so much for the reply and help, I didn't think I would get a response so fast.

I have tested, below screenshot is the changes I made off your suggestion, and its still deleting the Story and Subtask when the Story has no time but each Subtask has 1 hour in it.

For testing I am doing 1 Story with no time that has 2 Subtasks with 1 hour each.

![Screenshot 2024-08-30 154601.png](https://community.atlassian.com/t5/image/serverpage/image-id/344875i56B091C70BC3A7C5/image-size/large?v=v2&px=999 "Screenshot 2024-08-30 154601.png")

Maybe I did something incorrect?

This is Jira Cloud also, I forgot to mention this in my first post.


author: Trudy Claspill
body: Hello [@Jason Krewson](/t5/user/viewprofilepage/user-id/5437316)

It looks like you are missing a curly brace before **triggerIssue** in the JQL also - there should be two curly braces.

![Screenshot 2024-08-30 at 2.07.04?PM.png](https://community.atlassian.com/t5/image/serverpage/image-id/344882i8D2BD1125DBC85A2/image-size/large?v=v2&px=999 "Screenshot 2024-08-30 at 2.07.04?PM.png")

After fixing that, please test it again.

If it still does not work correctly then there may be something else wrong with the JQL.

Is the project to which the rule applies a Company Managed project or a Team Managed project? The subtask issue type name in the Lookup Issues JQL needs to be correct for that project type.

Can you confirm that the Lookup Issues JQL is finding the subtasks by substituting the issue key of your test issue where the JQL currently has {{triggerIssue.key}}, and then click the Validate Query link below and to the right.

![Screenshot 2024-08-30 at 2.10.42?PM.png](https://community.atlassian.com/t5/image/serverpage/image-id/344883iB3F316581ED4760C/image-size/large?v=v2&px=999 "Screenshot 2024-08-30 at 2.10.42?PM.png")

If that says there are 0 issue then you should go to the View All Issues page and work out the JQL details to make sure it is correct for retrieving the subtasks you expect. The only difference between the JQL there and in the rule would be the use of the smart value for the trigger issue key.

If it still doesn't work, please share new images of the changes you've made and share the rule execution log output


author: Jason Krewson
body: It seems to be this part that is causing the issue, moment I remove it I can validate the JQL query, if I leave it in I cannot click on validate query.

**parent = {{triggerIssue.key}} and**

This is a global rule but all our projects are company managed.

When I look through smart values, I cannot find {{triggerIssue.key}}.

![1.png](https://community.atlassian.com/t5/image/serverpage/image-id/344884iA5F43A15EE57141B/image-size/large?v=v2&px=999 "1.png")![2.png](https://community.atlassian.com/t5/image/serverpage/image-id/344885iCCC26579AECBEEB2/image-size/large?v=v2&px=999 "2.png")


author: Trudy Claspill
body: Hello [@Jason Krewson](/t5/user/viewprofilepage/user-id/5437316)

The Validate Query can't be used when you have a smart value in the query. That is why I asked you to substitute in the actual issue key as a test.
> Can you confirm that the Lookup Issues JQL is finding the subtasks by **substituting the issue key of your test issue where the JQL currently has {{triggerIssue.key}},** and then click the Validate Query link below and to the right.

Information about the smart value {{triggerIssue}} can be found here:

<https://support.atlassian.com/cloud-automation/docs/jira-smart-values-issues/#--triggerIssue-->

It is a way to refer to the issue that triggered the rule, regardless of the current context of the rule at the moment of the reference. You can use {{triggerIssue}} anywhere that you could use {{issue}} to get at the values in the rule's trigger issue.

Did you go to the View All Issues page to validate that the JQL, with an actual issue key after **parent=**, returns the subtasks you would expect for the specified issue?


author: Jason Krewson
body: Thanks for the information on this, it does return the correct subtasks when I swap it out with one of the parents.

parent = DELETE-20 and issuetype = Sub-task and Timespent is not empty

I tested again moving the parent Delete-20 to To Be Deleted status and it deleted the Story and the Subtasks.

We do use Tempo for time tracking, but this does add time to Timespent, that is why the automation works for just the Story (and JQL above for Subtasks), just couldn't figure it out for the Subtasks, but we are on the right track.


author: Trudy Claspill
body: So for DELETE-20 there were Sub-tasks for which Timespent was not empty, and it was deleted anyhow by the rule?

Please share the rule execution audit log details from the rule execution where DELETE-20 and its subtasks were deleted.


author: Jason Krewson
body: Correct and below is the log from when it deleted it.

![Screenshot 2024-08-30 191312.png](https://community.atlassian.com/t5/image/serverpage/image-id/344899i1BBADE9EE4335975/image-size/large?v=v2&px=999 "Screenshot 2024-08-30 191312.png")


author: Trudy Claspill
body: According to the log the issue is not matching the conditions on the IF, so it moves on to the ELSE-IF which it does match and the issue gets deleted.

In the IF it looks like for the smart value you may have used {{lookupissues... with a lower case "i". It needs to be an upper case "I". If it is not upper case please make that change and test again.

If that does not resolve the issue, again please provide the log output and also please an image of the details of the IF conditions.


author: Jason Krewson
body: Yep it was i not I, so I updated it to the below.

{{lookupIssue.size\|0}}

It ended up deleting it again.

![Screenshot 2024-08-30 200250.png](https://community.atlassian.com/t5/image/serverpage/image-id/344902iD4F11C1D8A2B61F2/image-size/large?v=v2&px=999 "Screenshot 2024-08-30 200250.png")


author: Trudy Claspill
body: It also has to be plural, as in the example I provided.

{{lookupIssues.size\|0}}


author: Jason Krewson
body: Good catch, I wont't be able to update this and test again untill Thursday. Thanks for all your help on this, I will update again the moment I test and will let you know the results.


author: Jason Krewson
body: [@Trudy Claspill](/t5/user/viewprofilepage/user-id/3569011) I just got back from vacation, updated "{{lookupIssues.size\|0}}" correctly, tested, and it worked!

I will do some more testing and let you know but this is great!


author: Jason Krewson
body: Testing went well, I tested every combination I could think of and it only deleted when there as no time in the Story and the subtask so its perfect.

Thanks again for all the help [@Trudy Claspill](/t5/user/viewprofilepage/user-id/3569011)

Below is what the final automation looks like.

![Screenshot 2024-09-05 123353.png](https://community.atlassian.com/t5/image/serverpage/image-id/345834i53E40BE846C6D30C/image-size/large?v=v2&px=999 "Screenshot 2024-09-05 123353.png")


Tags: automation

cloud

cloud-automation

jira-cloud

time

time-spent