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

Jean-Francois Bouchard April 1, 2016

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
Thanos Batagiannis [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 1, 2016

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
Jean-Francois Bouchard April 2, 2016

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?

Steven F Behnke
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 2, 2016

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 February 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
TIS Admin August 9, 2019

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
AUG Leaders

Atlassian Community Events