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

1 vote
Accepted answer
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!

 

0 votes

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 yesterday in Jira Service Desk

Looking for anyone who has switched from Zendesk to Jira Service Desk

Hi Community! The Jira Service Desk marketing team is looking for customers who have successfully switched from Zendesk to Jira Service Desk!   We’d love to hear your thoughts on the pros and ...

29 views 0 1
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