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

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

This widget could not be displayed.

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

This widget could not be displayed.
Alexey Matveev Community Champion Nov 30, 2017

Hello,

What plugin do you have for writing scripts?

Hi Alexey, 

 

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

Alexey Matveev Community Champion Nov 30, 2017

Your Jira version is Server, is not it?

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

Alexey Matveev Community Champion Nov 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?

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 Community Champion Nov 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.

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 Community Champion Nov 30, 2017

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

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:(

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 Community Champion Nov 30, 2017

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

it did. it is Expense with a capital E.

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

Alexey Matveev Community Champion Nov 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.

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") }

 

This widget could not be displayed.

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?

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Published Apr 22, 2018 in Jira Software

How-to setup a secured Jira Software 7.9.0 on Ubuntu 16.04.4 in less than 30 minutes

...PermissionsStartOnly=true User=www-data Group=www-data ExecStart=/opt/jira/bin/startup.sh ExecStop=/opt/jira/bin/shutdown.sh TimeoutStartSec=120 TimeoutStopSec=600 PrivateTmp=true [Install] WantedBy...

1,030 views 5 9
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