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

0 votes

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.  

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
Community showcase
Posted Oct 11, 2018 in Marketplace Apps

You + one app + a desert island...

Hi all! My name is Miles and I work on the Marketplace team. We’re looking for better ways to recommend and suggest apps that are truly crowd favorites, so of course we wanted to poll the Community. ...

2,559 views 5 6
Join discussion

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