Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
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

Custom field calculated from other drop-down-fields Edited

Hello Atlassian Community,

 

I am trying to get a custom field that is based on a result from 5 more custom fields values (Drop-down Single). In the past I did this with Power Custom Fields SIL but now it's paid and we don't want to spend 2000$ only for this field. We have Adaptavist Scriptrunner license and I am sure we can do it with it? 
So I have 5 custom fields with options: High, Medium, Low. Each of them represent a number (3,2,1). 
The goal is that my custom field will show a value based on the SUM of these fields.

This should be really simple if you look at my code: 

def damage = getCustomFieldValue("Damage")
def reproducibility = getCustomFieldValue("Reproducibility")
def exploitability = getCustomFieldValue("Exploitability")
def affectedUsers = getCustomFieldValue("Affected users")
def discoverability = getCustomFieldValue("Discoverability")

def damagerating;
def reproducibilityrating;
def exploitabilityrating;
def affectedUsersrating;
def discoverabilityrating;
def scoreTotal;

if ( damage == "Low" ){
damagerating = 1;
} else if ( damage == "Medium" ){
damagerating = 2;
} else if ( damage == "High" ){
damagerating = 3;
}

if ( reproducibility == "Low" ){
reproducibilityrating = 1;
} else if ( reproducibility == "Medium" ){
reproducibilityrating = 2;
} else if ( reproducibility == "High" ){
reproducibilityrating = 3;
}

if ( exploitability == "Low" ){
exploitabilityrating = 1;
} else if ( exploitability == "Medium" ){
exploitabilityrating = 2;
} else if ( exploitability == "High" ){
exploitabilityrating = 3;
}

if ( affectedUsers == "Low" ){
affectedUsersrating = 1;
} else if ( affectedUsers == "Medium" ){
affectedUsersrating = 2;
} else if ( affectedUsers == "High" ){
affectedUsersrating = 3;
}

if ( discoverability == "Low" ){
discoverabilityrating = 1;
} else if ( discoverability == "Medium" ){
discoverabilityrating = 2;
} else if ( discoverability == "High" ){
discoverabilityrating = 3;
}


scoreTotal = (damagerating + reproducibilityrating + exploitabilityrating + affectedUsersrating + discoverabilityrating)

if (scoreTotal >= 10) {
return "High";
}

if (scoreTotal >= 5) {
return "Medium";
}

if (scoreTotal < 5) {
return "Low";
}
The field is not even show in the Screen where it's configured. When I try the Preview I get"
Result: null
Log: 
2019-08-09 09:35:49,144 ERROR [runner.ScriptFieldPreviewRunner]: ************************************************************************************* 2019-08-09 09:35:49,144 ERROR [runner.ScriptFieldPreviewRunner]: Script field preview failed for field that has not yet been created groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getCustomFieldValue() is applicable for argument types: (java.lang.String) values: [Damage] at Script163.run(Script163.groovy:1)


Highly appreciate any help :) 

1 answer

1 accepted

1 vote
Answer accepted

rewrite the first "paragraph" of your code like this:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager
def damageField = customFieldManager.getCustomFieldObjectsByName("Damage")[0]
def damage = issue.getCustomFieldValue(damageField)?.value

I think you got the idea and can apply it to other fields. Basically, you need to get the object representing your custom field first, and then get it's value from the issue.

Hi Ilya,  thank you first! So... there is a progress! 

I am now able to get the values from the custom fields using this code: 

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

def customFieldManager = ComponentAccessor.customFieldManager

def damageField = customFieldManager.getCustomFieldObjectsByName("Damage")[0]
def damage = issue.getCustomFieldValue(damageField)
Now the problem is with this part that converts the value from string to number: 

if ( damage == "Low" ){
damagerating = 1;
} else if ( damage == "Medium" ){
damagerating = 2;
} else if ( damage == "High" ){
damagerating = 3;
}

 Now it returns proper result for "damage', but for damagerating it returns null

Do you have idea?

if it's single select type custom field, you should use 

issue.getCustomFieldValue(damageField)?.value

because just 

issue.getCustomFieldValue(damageField)

gives you Option, not the String, which you want to compare it to

Like TIS Admin likes this

This is what I get when I put "?.value"

jiravalue.png

 

Also it really returns the proper value of this field for the previewed issue.

because we are using dynamic typing here, it doesnt instantly knows that the customFieldValue we are getting here is Option type (which has the value property)

I'd say, most times you can ignore static type checking errors if you have idea what you are doing (like in this case, Im sure that "damage" field is select list, so it's value gonna have a value property and i can safely ask for it)

Like TIS Admin likes this

Thank you very much! It works now regardless the checking :) 

I wish you an awesome weekend, you saved me! 

Hi TIS Admin,

Please check my below script , I am getting an error at the declaration:

I want a calculated value from 2 custom fields, so created a script field for it and written down the below script:

Please suggest your inpits on it, as its not working.

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.jira.issue.Issue

 

Issue mainIssue = issue

def customFieldManager = ComponentAccessor.getCustomFieldManager()

 

def riskimpact = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_18484".getValue())) as Integer

 

def riskoccprobablity = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_18483".getValue())) as Integer

 

def risklevel = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_23091".getValue())) as Integer

 

if ( ( riskimpact * riskoccprobablity) == 0)

{

    return risklevel == 0

}

else if  ( ( riskimpact * riskoccprobablity) < 4)

{

    return risklevel == 1

}

else if  ( ( riskimpact * riskoccprobablity)  < 8 )

{

    return risklevel == 2

}  

else

{

    return risklevel == 3

}

Suggest an answer

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

JSM June Challenge #2: Share how your business teams became ITSM rockstars

For JSM June Challenge #2, share how your non-technical teams like HR, legal, marketing, finance, and beyond started using Jira Service Management! Tell us: Did they ask to start using it or...

263 views 7 7
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