Forums

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

Budget Calculation for Epic Using Child Issues - ScriptRunner

Jared Sylvia
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 31, 2024

I want to use my Procurement Dept.'s project Epics to track budget information and perform sum and difference calculations for the Overall Cost assigned to the Budget, and Remaining Balance respectively.

I have used some script examples in the Adaptavist library to help me get to this point, and want to make sure I am not missing anything. 

The goals are as follows:

  • A4J will edit the Epic Link of 'Purchase' issue types based on a custom field selection
  • Each 'Purchase' issue has a custom field Cost that will be defined later by the Procurement Agents
  • I want to sum each Cost as the Epic Link is edited and set the Epic custom field Overall Cost as each issue is linked
  • The Epic should then use a static Budget custom field to calculate the difference between Budget and Overall Cost, and set the value of a final custom field, Remaining Balance

Rule Structure

Rule Structure.png

 


Below is the script I was able to compile from library examples. Looking for confirmation this will work as expected, and help setting the Remaining Balance field value.

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.Issue

// The issue type for which we want the scripted field to be displayed

final issueTypeName = 'Epic'

// The linked issues with that issue type will used

final linkedIssueType = 'Purchase'

// The values of that custom field - of type number - we want to sum up

final overallCostName = 'Cost'

if (issue.issueType.name != issueTypeName) {

return null

}

def linkedIssues = ComponentAccessor.issueLinkManager.getOutwardLinks(issue.id).findAll { it.destinationObject.issueType.name == linkedIssueType }

if (!linkedIssues) {

return null

}

def overallCost = ComponentAccessor.customFieldManager.getCustomFieldObjects(linkedIssues.first().destinationObject).findByName(overallCostName)

if (!overallCost) {

log.debug "Custom field is not configured for that context"

return null

}

linkedIssues*.destinationObject.sum { Issue it -> it.getCustomFieldValue(overallCost) ?: 0 }

// Get the required component

def customFieldManager = ComponentAccessor.customFieldManager

// The name of the Overall Cost

final overallCostFieldName = "Overall Cost"

// The name of the Budget

final budgetFieldName = "Budget"

// Get the custom field objects

def overallCostField = customFieldManager.getCustomFieldObjects(issue).find { it.name == overallCostFieldName }

def budgetField = customFieldManager.getCustomFieldObjects(issue).find { it.name == budgetFieldName }

if (!overallCostField || !budgetField) {

log.info "Could not find one ore more of the provided custom fields"

return null

}

// Get the values from the Epic

def overallCostValue = issue.getCustomFieldValue(overallCostField) as Number

def budgetValue = issue.getCustomFieldValue(budgetField) as Number

//Set the Remaining Balance value of the the Epic

def remainingBalance = budgetValue - overallCostValue as Number

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
9.4.14
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events