Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How do I calculate a field based on linked issues?

Scott Federman
Contributor
November 14, 2017

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
Scott Federman
Contributor
November 17, 2017

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?

David Fischer
Community Champion
November 17, 2017

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.

0 votes
David Fischer
Community Champion
November 14, 2017

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

Scott Federman
Contributor
November 17, 2017

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.

David Fischer
Community Champion
November 17, 2017

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;
Scott Federman
Contributor
November 30, 2017

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?

David Fischer
Community Champion
November 30, 2017

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

Scott Federman
Contributor
November 30, 2017

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events