Count number of Linked Stories to Custom Issue Type
Asked by Petar Cvetanovic:

Hi I need help with automatically counting the number story-ies connected to custom issue type Project  

The number should change whenever i attach new story or detach a story from custom issue type "Project"  

check the video for a detailed explanation -

<https://www.loom.com/share/2d9f225dbfe4460faebb47a42bb2101d>


Answers:
author: John Funk
body: Hi Petar,

Sure, you can do that. Create an automation rule base on an Issue Created.

The add a condition where Issue Type = Story

Then add an action for lookup issues.

Use this for the value for the lookup issues: parent = {{triggerIssue.parent}}

Then add an action for Create Variable. Variable name: TotalAssets. Smart value is: {{lookupIssues.size\|0}}

Then add a Branch for Parent

Next add an action under the branch for Edit Issue. Choose the Total Assets field.

In the blank area under the field name put: {{TotalAssets}}

Update/Save/Give the rule a name. Then test it out!

comments: author: Petar Cvetanovic
body: I got this error message.

![Screenshot 2024-07-15 at 11.19.28.png](https://community.atlassian.com/t5/image/serverpage/image-id/335598iEC56C920B2877413/image-size/large?v=v2&px=999 "Screenshot 2024-07-15 at 11.19.28.png")

See the video of the whole automation - <https://www.loom.com/share/0339608f0ea54ee4b05b9aaa8641d3b5>


author: Bill Sheboy
body: Hi [@Petar Cvetanovic](/t5/user/viewprofilepage/user-id/5181502)

*First thing, "project" already has a specific meaning in Jira, and so creating an issue type with that same name may have unintended consequences (e.g., cause confusion, lead to automation rule or JQL errors, etc.). Please use cause in automation rules to confirm the context is correct when checking for the issues.*

Please post an image of your complete automation rule for context. That will help the community to offer better suggestions.

Until we see that...

The error you show in the audit log indicates *this rule triggered for an issue which does not have a parent*. To solve that symptom, add a condition after the trigger to check if the {issue.parent}} is not empty, or restructure the rule so it only proceeds when there is a parent:

* trigger: issue created
* action: re-fetch issue
* condition: issue type equals *Story*
* branch: to parent
  * condition: issue type equals *Project*
  * action: lookup issues, with JQL parent={{issue.key}} AND issueType=Story
  * action: edit the issue to set the field to {{lookupIssues.size\|0}}

*Please note well*: this only handles the scenario of when a new story is created and set to a parent at the same time. If you want this count to be accurate, additional rules will be needed for other scenarios:

1. Story created with a parent Project (covered by the rule above)
2. Story's parent added later
3. Story's parent changed from one Project to another
4. Story is deleted
5. a Story's issue type changes such that it no longer fits as a child of the Project
6. a Project's issue type changes such that your count field is no longer relevant
7. issues with types other than Story are children added to a Project issue; what happens to the count?
8. etc.

Kind regards,  
Bill


author: Petar Cvetanovic
body: Can we automate the opposite way? I need all use cases covered, and I think the opposite way is a lot easier.   

How I would do it logically  

Trigger - on the transition to status "Scheduled"

Condition - issue type project  

Action 1 - Lookup all issues that are (child off parent) of linked issue type and  

Actopm 2 -vCreate a variable {{TotalAssets}} that is equal to the lookup issue count  

Action 3 - Set Custom Issue Field Vavlue {{TotalAssets}}  

I think this is an easier solution than what you suggested. I will just need help with looking up and counting.   

See video exp -https://[www.loom.com/share/bb1ef14342ad4d45adc2268de6a69580](http://www.loom.com/share/bb1ef14342ad4d45adc2268de6a69580)  

Screenshot of the suggested rule

![Screenshot 2024-07-16 at 11.32.54.png](https://community.atlassian.com/t5/image/serverpage/image-id/335841i6F0B5E50BC9E80F6/image-size/large?v=v2&px=999 "Screenshot 2024-07-16 at 11.32.54.png")


Tags: cloud

jira-cloud