Not able to calculate the Scripted field value

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!

2 answers

1 accepted

This widget could not be displayed.
Vasiliy Zverev Community Champion Dec 02, 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 + "%" ;;
}

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 Community Champion Dec 02, 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 + "%" ;;
}

Hi Vasiliy Zverev,

 

Getting below error while using the script :

Cannot invoke method getCustomFieldValue() on null object

 

Please suggest. Thanks!

 

This widget could not be displayed.

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
Community showcase
Posted Tuesday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

165 views 1 3
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you