Not able to calculate the Scripted field value

Avinash Gautam December 2, 2016

Hi Nic Brough [Adaptavist],

 

I wanted to create a scripted field for calculating two field values

 

First field  Severity Of Impact(drop down) : Very high, High, Medium, Low, Very Low     //customfield_14531

Second field Probability Of Occurence(drop down) : 10, 20, 30 .. 100                           //customfield_14532

 

Required Scripted field is  Calculated field = Severity Of Impact * Probablity of occurance

 

I have tried the below script but it's not working

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_14531")
def cField1 = customFieldManager.getCustomFieldObject("customfield_14532")
def soi = issue.getCustomFieldValue(cField)
def poc = issue.getCustomFieldValue(cField1)

if (soi == "Very Low")
return "Exposure = " + 1*poc / 100 + "%" ;
else if (soi == "Low")
return "Exposure = " + 2*poc / 100 + "%" ;
else if (soi == "Medium")
return "Exposure = " + 3*poc / 100 + "%" ;
else if (soi == "High")
return "Exposure = " + 4*poc / 100 + "%" ;
else
return "Exposure = " + 5*poc / 100 + "%" ;

 

 

The above script is throwing error and not working. Please help to solve this. Thanks!

3 answers

1 accepted

1 vote
Answer accepted
Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 2, 2016

Here is refactored version of your code. I assume that custom field with id14532 is number type.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def poc = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_14532"))

switch(issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_14531"))){
    case "Very Low":    return "Exposure = " + 1*poc / 100 + "%" ;
    case "Low":         return "Exposure = " + 2*poc / 100 + "%" ;
    case "Medium":      return "Exposure = " + 3*poc / 100 + "%" ;
    case "High":        return "Exposure = " + 4*poc / 100 + "%" ;
    default:            return "Exposure = " + 5*poc / 100 + "%" ;;
}
Avinash Gautam December 2, 2016

Hi Vasiliy Zverev,

 

Yes, it's working but i am having just a concern that the number field could be a dropdown with number like 10, 20....100. I have created the field like that but as per your script, i have created another one with number field and it started working. So could you please make it feasible for drop down field as well. Thanks for your help!

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 2, 2016

Here is version for custom field of dropdown type:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.Option

MutableIssue issue;

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
double poc = new Double ( ( (Option) issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_14532") ) ).getValue() ).doubleValue()

switch( issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_14531"))){
    case "Very Low":    return "Exposure = " + 1*poc / 100 + "%" ;
    case "Low":         return "Exposure = " + 2*poc / 100 + "%" ;
    case "Medium":      return "Exposure = " + 3*poc / 100 + "%" ;
    case "High":        return "Exposure = " + 4*poc / 100 + "%" ;
    default:            return "Exposure = " + 5*poc / 100 + "%" ;;
}
Avinash Gautam December 4, 2016

Hi Vasiliy Zverev,

 

Getting below error while using the script :

Cannot invoke method getCustomFieldValue() on null object

 

Please suggest. Thanks!

 

0 votes
Christina Hiller June 12, 2019

HI all

I'm trying something similar. I want to calculated a scripted field "Benefit Score" using  3 custom fields that are drop downs containing numeric values.  Both approaches I've tried have issues.  ~~~~Caveat here... I'm no developer, so any explanation as to why I should change what I should change would be appreciated.  Thanks in advance!

Approach 1:   Using the code below, I receive the following error when it tries to run. "java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object"

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.Option

MutableIssue issue

def valueFactor = 12
def techFactor = 13
def maintFactor = 8

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
double valueScore = new Double ( ( (Option) issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_18705") ) ).getValue() ).doubleValue()

double techScore = new Double ( ( (Option) issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_18703") ) ).getValue() ).doubleValue()

double maintScore = new Double ( ( (Option) issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_18704") ) ).getValue() ).doubleValue()

return (valueFactor * valueScore) + (techFactor * techScore)+ (maintFactor * maintScore)

 error log:

2019-06-12 13:29:51,644 ERROR [customfield.GroovyCustomField]: *************************************************************************************
Script field failed on issue: CMS-27353, field: Benefit Score
java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object
 at Script112.run(Script112.groovy:14)

 

Approach 2:

I'm guessing this approach doesn't work because I'm using drop down values, however I'm not sure the correct way to adjust.

import com.atlassian.jira.component.ComponentAccessor
def valueFactor = 12
def techFactor = 13
def maintFactor = 8
def valueScore = getCustomFieldValue("Delivered Value Score")
def techScore = getCustomFieldValue("Trending Technology Score")
def maintScore = getCustomFieldValue("Platform Maintenance Score")


// check both fields contain valid numeric values
if (valueScore == null) { valueScore == 0 }
if (techScore == null) { techScore == 0 }
if (maintScore == null) { maintScore == 0 }

if (valueScore != null && techScore != null && maintScore != null ){
return (valueFactor * valueScore) + (techFactor * techScore)+ (maintFactor * maintScore)

} else {

// return to some code to indicate a null value in one of the fields
return "0"
}

 

benefit score issue.png

Christina Hiller June 12, 2019

Piecing together a bunch of other posts I figured it out. :-)   Here's what worked.  Doesn't handle defining null value as 0 like I wanted, but I can work around this and make the score fields required.  

 


import com.atlassian.jira.component.ComponentAccessor
import java.text.NumberFormat

def valueFactor = 21
def techFactor = 13
def maintFactor = 8
def valueScore = getCustomFieldValue("Delivered Value Score")?.getValue()
def techScore = getCustomFieldValue("Trending Technology Score")?.getValue()
def maintScore = getCustomFieldValue("Platform Maintenance Score")?.getValue()

// check both fields contain valid numeric values
if (valueScore == null) { valueScore == 0 }
if (techScore == null) { techScore == 0 }
if (maintScore == null) { maintScore == 0 }

if (valueScore != null && techScore != null && maintScore != null ){
return ( Integer.parseInt(valueScore) * valueFactor) + ( Integer.parseInt(techScore) * techFactor) + ( Integer.parseInt(maintScore) * maintFactor)

} else {

// return to some code to indicate a null value in one of the fields
return "0"
}
0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 2, 2016

It usually helps if you fix the error.  But I can't tell you how to do that - as far as I know, from your question, the error might be "help help, I'm being attacked by penguins".

Suggest an answer

Log in or Sign up to answer