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

This widget could not be displayed.

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?

Steven Behnke Community Champion Apr 02, 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)

@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 

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted yesterday in Jira

Atlassian Research Workshop opportunity on Sep. 28th in Austin, TX

We're looking for participants for a workshop at Atlassian! We need Jira admins who have interesting custom workflows, issue views, or boards. Think you have a story to sha...

58 views 2 2
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