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