Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
Level
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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!

3 answers

1 accepted

1 vote
Answer accepted

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!

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".

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

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"
}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

ThinkTilt is joining the Atlassian Family!

This morning, Atlassian announced the acquisition of ThinkTilt , the maker of ProForma, a no-code/low code form builder with 700+ customers worldwide. ThinkTilt helps IT empower any team in their or...

267 views 16 17
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you