Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,461,227
Community Members
 
Community Events
176
Community Groups

How to get a string value from a drop down custom field (Groovy Scripted Field)

Hi everyone,

The situation is this:
I have 4 custom fields which contain values (Complete, Incomplete, Partial).
I need a 5th field, the result field, which will give me a score (Poor, Average, Good, Very Good, Excellent) based on the average of points calculated from the first 4 fields.

I have assigned points as follows:

  • Incomplete: 0 pts
  • Partial: 50 pts
  • Complete: 100 pts 

For the result field, I am using a Scripted Field.
Just to be clear, it looks like this when you create a new field.

scriptedfield.png 


This is what I've written up so far:

// We declare our variables and pull the data from the ticket
def stringContent = issue.get("customfield_19687"); 
def stringCategory = issue.get("customfield_19688");
def stringSolution = issue.get("customfield_19689");
def stringClarity = issue.get("customfield_19690");
def scoreContent;
def scoreCategory;
def scoreSolution;
def scoreClarity;
def scoreTotal;

// Storing the score from each drop down into a numeric variable
if (stringContent == "Incomplete") {
	scoreContent = 0;
} else if (stringContent == "Complete") {
	scoreContent = 100;
} else {
	scoreContent = 50;
}
if (stringCategory == "Incomplete") {
	scoreCategory = 0;
} else if (stringCategory == "Complete") {
	scoreCategory = 100;
} else {
	scoreCategory = 50;
}
if (stringSolution == "Incomplete") {
	scoreSolution = 0;
} else if (stringSolution == "Complete") {
	scoreSolution = 100;
} else {
	scoreSolution = 50;
}
if (stringClarity == "Incomplete") {
	scoreClarity = 0;
} else if (stringClarity == "Complete") {
	scoreClarity = 100;
} else {
	scoreClarity = 50;
}

// We refresh our total score variable
scoreTotal = (scoreContent + scoreCategory + scoreSolution + scoreClarity) / 4;

// We set our field value depending on the total score
if (scoreTotal <= 50) {
	return "Poor";
} else if (scoreTotal < 75) {
	return "Average";
} else if (scoreTotal < 84) {
	return "Good";
} else if (scoreTotal < 90) {
	return "Very good";
} else {
	return "Excellent";
}

 

Currently, the field does not show because it has no results.

I already tested the calculations and everything by replacing the first part and assigning values to the variables directly in the code

def stringContent = "Complete"; 
def stringCategory = "Complete";
def stringSolution = "Incomplete";
def stringClarity = "Patrial";

and it seems to work fine.

Which leads me to believe this part is the culprit:

def stringContent = issue.get("customfield_19687"); 
def stringCategory = issue.get("customfield_19688");
def stringSolution = issue.get("customfield_19689");
def stringClarity = issue.get("customfield_19690");

 

Am I even doing the right thing here? How do I get the values from my custom fields?

Thanks!

1 answer

1 accepted

3 votes
Answer accepted

Hi Jean-Francois

The right way to get the value of a custom field is 

getCustomFieldValue("Name or Id or of your custom field")

or if you use one of the free SR versions (<= 3.1.4) and your custom field is a Single Select List

def value = getCustomFieldValue("Name or Id or of your custom field")?.value

Thanks for the answer Thanos!

I managed to get it to work by using the field name:

def stringContent = getCustomFieldValue("Content score");

I have tried different ways to use the ID but haven't managed it.

def stringContent = getCustomFieldValue("19687");
def stringSolution = getCustomFieldValue("customfield_19689");
def stringClarity = getCustomFieldValue("cf[19690]");

How do you use the ID exactly?

I think I usually capture fields-by-ID by declaring them as field objects – 

def cf = customFieldManager.getCustomFieldObject("customfield_10030")
def string = getCustomFieldValue(cf)
Deleted user Feb 14, 2018

@Thanos Batagiannis [Adaptavist] When I add 

?.value  

 then it eats up a lot of CPU power. It causes JIRA to stop working. Is there any other soloution how to get a Value from a Custom Dropdown field 

Like # people like this

Hello There,

I am trying to achieve exactly the same. I have a Script Field (Text Field) with the following 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";
}

I think it's really straight forward what I want to achieve and show in this simple case. I will appreciate any advises.

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)
Like Nasos Nathanail likes this

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events