Set business value field = value of another field?

Linus Lindroth December 15, 2013

Hi

I need to set the "Business Value" field equal to a scripted field I have created with Script Runner to be able to see that calculated value in the backlog sorting. This is to automatically get updates of the value without having to export, edit in Excel and then import again.

I have been trying to figure out if I can use Javascripts but I don't know how that works. I've seen that it sould be possible to add javascripts in the description of the Business Value field, is that right? The script shoud be very simple since the value should be equal to another value, nothing more complicated than that but I need a little help how such a script would look like.

Thx

2 answers

1 accepted

0 votes
Answer accepted
Linus Lindroth December 16, 2013

I found the solution myself :-)

In the same scripted field where I calculate my "Calculated Priority" I found the syntax of how to set the Business Value field to the same. Here is the syntax needed for the scripted field

import org.apache.log4j.Category
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder

//Getting the value from another Custom select list field
def MaturityValue = getCustomFieldValue("Maturity Level")

//Getting the value from two build in fields
def DueDateValue = issue.dueDate
def EstimateValue = issue.originalEstimate

def MLFactor = 4
def DDFactor = 10
def EEFactor = 1
def MLValue = 0
def DDValue = 0
def EEValue = 0

//Setting todays date for calculating time to due date
def today = new Date()

//Setting the proper Maturity Level value (Value1, 2 etc are fake, change to something real)
if (MaturityValue.toString() == "Value1") {
MLValue = 7
}
else if (MaturityValue.toString() == "Value2") {
MLValue = 6
}
else if (MaturityValue.toString() == "Value3") {
MLValue = 5
}
else if (MaturityValue.toString() == "Value4") {
MLValue = 4
}
else if (MaturityValue.toString() == "Value5") {
MLValue = 3
}
else if (MaturityValue.toString() == "Value6") {
MLValue = 2
}
else if (MaturityValue.toString() == "Value7") {
MLValue = 1
}
else {
MLValue = 0
}

//Setting the proper Due Date value, values in days
//If less then 4 weeks
if (DueDateValue-today < 28 ) {
DDValue = 7
}
//If 4-6 weeks
else if ((DueDateValue-today >= 28) && (DueDateValue-today < 42)) {
DDValue = 6
}
//If 6-8 weeks
else if ((DueDateValue-today >= 42) && (DueDateValue-today < 56)) {
DDValue = 5
}
//If 8-10 weeks
else if ((DueDateValue-today >= 56) && (DueDateValue-today < 70)) {
DDValue = 4
}
//If 10-12 weeks
else if ((DueDateValue-today >= 70) && (DueDateValue-today < 84)) {
DDValue = 3
}
//If 12-16 weeks
else if ((DueDateValue-today >= 84) && (DueDateValue-today < 112)) {
DDValue = 2
}
//If 16-22 weeks
else if ((DueDateValue-today >= 112) && (DueDateValue-today < 154)) {
DDValue = 1
}
else {
DDValue = 0
}

//Setting the proper Estimated Effort Level value (hours)
if ((EstimateValue/3600 > 1000) || (EstimateValue/3600 == 0)) {
EEValue = 7
}
else if ((EstimateValue/3600 > 750) && (EstimateValue/3600 <= 1000)) {
EEValue = 6
}
else if ((EstimateValue/3600 > 400) && (EstimateValue/3600 <= 750)) {
EEValue = 5
}
else if ((EstimateValue/3600 > 200) && (EstimateValue/3600 <= 400)) {
EEValue = 4
}
else if ((EstimateValue/3600 > 100) && (EstimateValue/3600 <= 200)) {
EEValue = 3
}
else if ((EstimateValue/3600 > 40) && (EstimateValue/3600 <= 100)) {
EEValue = 2
}
else if ((EstimateValue/3600 > 20) && (EstimateValue/3600 <= 40)) {
EEValue = 1
}
else if ((EstimateValue/3600) <= 20) {
EEValue = 0
}
else {
EEValue = 0
}

//Needed?
log = Category.getInstance("com.onresolve.jira.groovy.CopyRegEx")

// The fieldID of Business Value in my JIRA instance - find your own value
// by hoovering over the Edit in Administration - Issues - Custom Fields
def targetFieldName = "customfield_10009"

ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
CustomField customFieldTarget = customFieldManager.getCustomFieldObject(targetFieldName)

//My calculated value I want to display
def sourceFieldVal = DDValue*DDFactor+EEValue*EEFactor+MLValue*MLFactor

IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
if (sourceFieldVal) {
customFieldTarget.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customFieldTarget), sourceFieldVal.toDouble()), changeHolder)
}

return sourceFieldVal as Double

0 votes
Linus Lindroth January 7, 2014

Jamie from Script Runner plugin didn't recommend this solution since setting another fields value from a scripted field could cause reindexing to fail. This was his answer:

You should not try to update the issue in a scripted field, as you won't be able to reindex the issue without getting in to an infinite loop. Use a listener.

However I don't really understand how to use a listener insted for the same purpose

Suggest an answer

Log in or Sign up to answer