How do I calculate a field based on linked issues?

I need to find a way to calculate budgets and costs. 

The parent would have the budget field and the child would its cost. I need a way to subtract the cost of the child from the budget in the parent so that it shows remaining budget. Budget(P)-Cost(C). 

Any help here would be greatly appreciated. 

2 answers

This widget could not be displayed.

Assuming the "parent" issue has linked ("child") issues linked through the "blocks" link type name, and that link type name is the "Outward Description" of the link type on the "Issue Linking" Jira admin page, you can use a calculated number field with the following formula:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.link.IssueLink;

if (issue.get("Budget") == null)
return null;
Double val = issue.get("Budget");
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField customField = customFieldManager.getCustomFieldObject("customfield_12345"); //change to field ID of the "Cost" field

for (IssueLink il : issue.get("issuelinks")) {
if (il.getIssueLinkType().getOutward().equals("blocks")) { //change to the appropriate "Outward Description"
Issue linkedIssue = il.getDestinationObject();
cost = linkedIssue.getCustomFieldValue(customField);
if (cost!=null)
val = val - cost;
}
}

return val;

 Hope this helps,

David

Hi David, 

 

Apparently there was an change in requirements. 

Budget field(11723)-Expense field (11724)=Remaining Budget field (11721). 

(Budget and Expense are number fields and Reamining Budget is a SIL field)

We would be using the linking type "Related" with an outward description of "is related to".

Any help on that one would be massively appreciated.

Hi Scott,

I'm not sure I understand.

  1. what is a "SIL" field?
  2. In what way is the requirement different from before?

With what you described, the code would be:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.link.IssueLink;

if (issue.get("customfield_11723") == null)
return null;
Double val = issue.get("customfield_11723");
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField customField = customFieldManager.getCustomFieldObject("customfield_11724");

for (IssueLink il : issue.get("issuelinks")) {
if (il.getIssueLinkType().getOutward().equals("is related to")) {
Issue linkedIssue = il.getDestinationObject();
cost = linkedIssue.getCustomFieldValue(customField);
if (cost!=null)
val = val - cost;
}
}

return val;

Hey David, 

I think im close but still having some issues. I changed the cost field to be "Total Expense which is customfield_11746. All the errors cleared except 1.

image.pngand it returns the following error

image.png

What am i missing here?

Hi Scott,

first of all, you are using ScriptRunner and not JMCF, so the syntax is different.

But anyway, variable names cannot contain spaces, nor be surrounded by quotes. That's your error here.

If you want to use ScriptRunner, you should ask Adaptavist for support.

Cheers,

David

ahh got ya. I have both. Ill create a calculated numeric field and see how that goes.  

This widget could not be displayed.

I changed cost to expense (which is no big deal) But budget is a static field. Remaining budget is where the calculation takes place. 

A SIL field uses SIL scripts. Would what you are suggesting be a typical numeric field using script runner or something like that?

Well, you asked the question with the JMCF tag, so I assumed you wanted to create a Calculated custom field. So this is what I offered: the script for a JMCF Calculated Number field.

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 9 hours ago in Marketplace Apps

The 7 hacks of highly successful automation

...there's anything I've learnt from working, it's that people are lazy! No offense to anyone reading this, but it's true and we can all admit it. The easier you make something for someone, the more...

54 views 0 8
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