Sum up fields in sub-tasks and show the result in story

Scott Federman November 30, 2017

Hey all!

 

So heres the scenario. I have a custom field in sub tasks called "expense" and a field in a story "called total" expense. When a sub-task is created the user will enter the expense. I want to have all the expenses summed up and totaled in the "total expense" field in the story. As sub tasks are added that total expense field needs to update. 

 

Anyone have any crazy ideas (or not so crazy ideas) to get this accomplished?

 

My issue is i don't know the first thing about writing scripts so i wouldn't know where to begin. 

3 answers

5 votes
Johnson Wang November 30, 2017

Hi @Scott Federman,

One other option for you to consider is the Power Scripts add-on. With much fewer lines of code, you can quickly and easily sum up fields in sub-tasks and show the result in story.

We've put together a quick 5 min video showing you how to set this all up. We've also included the sample script we used (it's performing simple subtraction, but you can change it to summation easily) so you can just copy and paste it to try it yourself.

Hope this helps!

Johnson

0 votes
Scott Federman November 30, 2017

Alright Alexey,

I have one final step in this project i could use your wizardry on. 

I have another issue type called Project Budget. This has a custom number field call "Budget".  What i want to do is link the story issue type to the project budget issue type using the "relates to" link type. Then i want to subtract the expense field we just created from the budget field in the project budget and return a "Remaining Budget" in the project budget issue type. 

 

Is that possible?

0 votes
Alexey Matveev
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.
November 30, 2017

Hello,

What plugin do you have for writing scripts?

Scott Federman November 30, 2017

Hi Alexey, 

 

I'm open to plugins. Script Runner, Powerscripts, etc. I will purchase the plugin based on getting this to work:)

Alexey Matveev
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.
November 30, 2017

Your Jira version is Server, is not it?

Scott Federman November 30, 2017

Yes server 7.5.0. We will be moving to cloud however in February

Alexey Matveev
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.
November 30, 2017

Do you know that Power Scripts exists only for Jira Server and Adaptivist Scriptrunner is limited for Cloud? And you would need another script for Cloud. Do you still need the script for Jira Server?

Scott Federman November 30, 2017

yes i would still need the script for server to satisfy the need now, but when i move to cloud i will need to update it accordingly. 

Alexey Matveev
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.
November 30, 2017

Ok.

0. Install Scriptrunner

1. Create custom field expense of number type

2. Create custom field total expense of scripted field type

3. Go to AddOns find scripted fields and push edit for total expense field.

4. Choose number template

5 paste the following script

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def csExpense = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("expense")
return issue.getSubTaskObjects().sum{it ->((Issue) it).getCustomFieldValue(csExpense)};

5. put total expense on the view screen of a task

6. put expense field on create, edit and view screen of sub-task.

7. fill expense field for a sub-task and total expense for the task will calculated automatically.

Scott Federman November 30, 2017

it does not seem to be pulling the expenses. I enter the expense in the sub task and the total expense field doesnt populate

Alexey Matveev
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.
November 30, 2017

I checked on my Jira Instance and it is working. Not sure why it is not working on yours

Scott Federman November 30, 2017

Just to make sure im one the same page. 

 

I created a new scripted custom field called "Total Expense" and placed it on the story view screen. 

 

I went to script runner and found scripted fields where i located the Total expenses field. 

I then selected the the number field option from the template drop down.

 

then i copied and pasted the following. 

image.png

I created a new sub task and entered the number in the expense field.

 

I refreshed the story and....nothing happened:(

Scott Federman November 30, 2017

Here is the error i get in the log

 

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2017-11-30 13:20:41,309 ERROR [customfield.GroovyCustomField]: *************************************************************************************
Script field failed on issue: OPS2-80, field: Total Expense
java.lang.NullPointerException
 at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
 at com.atlassian.jira.issue.Issue$getCustomFieldValue$5.call(Unknown Source)
 at Script9$_run_closure1.doCall(Script9.groovy:5)
 at Script9.run(Script9.groovy:5)
Alexey Matveev
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.
November 30, 2017

Did you create expense field? how is it called expense or Expense? it is case sensitive. In the script it is expense

Scott Federman November 30, 2017

it did. it is Expense with a capital E.

Scott Federman November 30, 2017

I update the script with the uppercase e and it works:) Thank you!

Alexey Matveev
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.
November 30, 2017

It is possible. It would be simpler if you made a field "Remaining Budget" which is a scripted field. Then in this field you would iterate over necessary links with a code like this
def issueLinks = ComponentAccessor.issueLinkManager.getInwardLinks(issue).findAll{it -> it.getIssueLinkType().getName().equals("relates to") }
Then you would iterate over issue links and get all issues with issue type Story and then for each story you would sum sub-task estimates. Then you would substract summed estimates from the budget field.

Scott Federman November 30, 2017

Im not sure i follow. I create a scripted field in the project budget issue type and use the script below?

def issueLinks = ComponentAccessor.issueLinkManager.getInwardLinks(issue).findAll{it -> it.getIssueLinkType().getName().equals("relates to") }

 

Suggest an answer

Log in or Sign up to answer