Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
  • Community
  • Q&A
  • Jira
  • Questions
  • MyGroovy custom calculated field created and compiles, but does not show up on the Issue type.

MyGroovy custom calculated field created and compiles, but does not show up on the Issue type.

Mili Mathew September 20, 2019

I've got MyGroovy add-on, similar to Scriptrunner, and I've been trying to use it to create a scripted custom field "risk" which calculates the value based on 

Impact X Probability - both these fields are single list type custom fields. 

The following is the groovy script I've used, which compiles fine, but the field is not appearing on the IssueType:

It is currently associated to screens for Epic, Story and Bugs. Am I missing some step in between? Any help is greatly appreciated! 

===================||======================

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import java.lang.Object
import java.lang.Double
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.onresolve.jira.groovy")
log.setLevel(Level.DEBUG)

def impact = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("TE Business Impact").toString()

def probability = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("TE Technical Complexity").toString()


def risk

if ((impact!="null") && (probability!="null")) {
risk = Double.parseDouble(impact) * Double.parseDouble(probability)
} else {
risk = null
}

return risk

3 answers

1 accepted

1 vote
Answer accepted
Sakshi Karnwal September 27, 2019

This is how we managed to get it working at Collinson:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import java.lang.Double

// needed to get custom fields
def customFieldManager = ComponentAccessor.getCustomFieldManager()

// get generic custom fields
def impact = customFieldManager.getCustomFieldObjectsByName("TE Business Impact").first()
def probability = customFieldManager.getCustomFieldObjectsByName("TE Technical Complexity").first()

// get instances for this issue
def impactValue = issue.getCustomFieldValue(impact)
def probabilityValue = issue.getCustomFieldValue(probability)

// get actual selected values
def selectedImpactValue = ((LazyLoadedOption)impactValue).getValue() as Double
def selectedProbabilityValue = ((LazyLoadedOption)probabilityValue).getValue() as Double

// calculate risk
def riskValue = null

if (selectedImpactValue && selectedProbabilityValue) {
riskValue = selectedImpactValue * selectedProbabilityValue
}

return riskValue

Mili Mathew September 27, 2019

Thanks. that works!

1 vote
Alexey Belostotskiy September 23, 2019

Hello,

You're not getting values this way:

ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("TE Business Impact").toString()

You're only getting the custom field. So your script is likely throwing errors.

Your script should look something like this:

import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.CustomFieldManager

@StandardModule
CustomFieldManager customFieldManager

Option impact = (Option) issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectsByName("TE Business Impact").first())
Option probability = (Option) issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectsByName("TE Technical Complexity").first())

if ((impact != null) && (probability != null)) {
//assuming you want to parse names of options as numbers
return Double.parseDouble(impact.value) * Double.parseDouble(probability.value)
}

return null 

 

Sakshi Karnwal September 27, 2019


This is how we managed to get it working at Collinson:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import java.lang.Double

// needed to get custom fields
def customFieldManager = ComponentAccessor.getCustomFieldManager()

// get generic custom fields
def impact = customFieldManager.getCustomFieldObjectsByName("TE Business Impact").first()
def probability = customFieldManager.getCustomFieldObjectsByName("TE Technical Complexity").first()

// get instances for this issue
def impactValue = issue.getCustomFieldValue(impact)
def probabilityValue = issue.getCustomFieldValue(probability)

// get actual selected values
def selectedImpactValue = ((LazyLoadedOption)impactValue).getValue() as Double
def selectedProbabilityValue = ((LazyLoadedOption)probabilityValue).getValue() as Double

// calculate risk
def riskValue = null

if (selectedImpactValue && selectedProbabilityValue) {
riskValue = selectedImpactValue * selectedProbabilityValue
}

return riskValue

Sakshi Karnwal September 27, 2019

This is how we managed to get it working at Collinson:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import java.lang.Double

// needed to get custom fields
def customFieldManager = ComponentAccessor.getCustomFieldManager()

// get generic custom fields
def impact = customFieldManager.getCustomFieldObjectsByName("TE Business Impact").first()
def probability = customFieldManager.getCustomFieldObjectsByName("TE Technical Complexity").first()

// get instances for this issue
def impactValue = issue.getCustomFieldValue(impact)
def probabilityValue = issue.getCustomFieldValue(probability)

// get actual selected values
def selectedImpactValue = ((LazyLoadedOption)impactValue).getValue() as Double
def selectedProbabilityValue = ((LazyLoadedOption)probabilityValue).getValue() as Double

// calculate risk
def riskValue = null

if (selectedImpactValue && selectedProbabilityValue) {
riskValue = selectedImpactValue * selectedProbabilityValue
}

return riskValue

0 votes
Mili Mathew September 20, 2019

Also just to add - I've checked the settings as below, the scripted field name is 'TE RiskScore' and contains the above script:

RiskScore field.PNG

Sakshi Karnwal September 27, 2019

This is how we managed to get it working at Collinson:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import java.lang.Double

// needed to get custom fields
def customFieldManager = ComponentAccessor.getCustomFieldManager()

// get generic custom fields
def impact = customFieldManager.getCustomFieldObjectsByName("TE Business Impact").first()
def probability = customFieldManager.getCustomFieldObjectsByName("TE Technical Complexity").first()

// get instances for this issue
def impactValue = issue.getCustomFieldValue(impact)
def probabilityValue = issue.getCustomFieldValue(probability)

// get actual selected values
def selectedImpactValue = ((LazyLoadedOption)impactValue).getValue() as Double
def selectedProbabilityValue = ((LazyLoadedOption)probabilityValue).getValue() as Double

// calculate risk
def riskValue = null

if (selectedImpactValue && selectedProbabilityValue) {
riskValue = selectedImpactValue * selectedProbabilityValue
}

return riskValue

Suggest an answer

Log in or Sign up to answer