Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Description

It returns the calculation of the Budget minus the Incurred Cost in the current project

Configuration

1. Create a Projectrak Number field called "Budget". 

2. Create a Projectrak Script field called "Incurred Cost"

3. Create a Projectrak Script field called "Remaining budget.

4. Insert the following Groovy script and modify according to your instance into "Remaining budget" field

Groovy Script

...

remaining-budget.groovy
linenumbers
Code Block
languagetruegroovy
package deiser.profields.scripts

import com.atlassian.jira.component.ComponentAccessor
import com.deiser.jira.profields.api.field.FieldService
import com.deiser.jira.profields.api.field.number.NumberField
import com.deiser.jira.profields.api.field.script.ScriptField
import com.deiser.jira.profields.api.value.ValueService
import java.text.NumberFormat

// Configuration
def BUDGET_FIELD_ID = 20
def INCURRED_COST_FIELD_ID = 22

// Components
def fieldService = ComponentAccessor.getOSGiComponentInstanceOfType(FieldService.class)
def valueService = ComponentAccessor.getOSGiComponentInstanceOfType(ValueService.class)
def jiraAuthenticationContext = ComponentAccessor.jiraAuthenticationContext


// Get the fields
def budgetField = fieldService.get(BUDGET_FIELD_ID)
def incurredCostField = fieldService.get(INCURRED_COST_FIELD_ID)

// Get the field values in the current project
def budget = valueService.getValue(project, (NumberField) budgetField) ?: 0
def incurredCost = valueService.getValue(project, (ScriptField) incurredCostField)?:"0"
def incurredCostBigDecimal = new BigDecimal(incurredCost)

// Returns the difference between the budget and the incurred cost
def numberFormat = NumberFormat.getInstance(jiraAuthenticationContext.locale)
numberFormat.maximumFractionDigits = 0
numberFormat.minimumFractionDigits = 0
numberFormat.groupingUsed = false
return numberFormat.format((budget - incurredCostBigDecimal)<0?0:(budget - incurredCostBigDecimal))

...

7. The result show be something like this: (please, keep in mind that this is only an example and your results should vary from what it is shown in the screenshot). 
Image Modified