Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner script field from another text custom field

Sam Heck
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 26, 2019

I will preface by saying I have little to no knowledge of scripts... But I am trying to create a script field that will return a text entry based on the text string in another custom field. Here is my code, which returns the "else" case every time:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.component.ComponentAccessor

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def type = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Risk Type"))
if (type == "New Feature/Function") {
return "Integrated Testing (manual or auto)"
}
if (type == "Logic change") {
return "Ad Hoc Testing (manual or auto)"
}
if (type == "UI change") {
return "Ad Hoc Testing (manual or auto)"
}
if (type == "No functional change") {
return "No testing required"
}
else {
return ""
}

1 answer

0 votes
Alex Christensen
Community Champion
April 26, 2019

Hey, Sam - a couple questions and things to check out:

  • What's the custom field type of the "Risk Type" field? When using .getCustomFieldValue() the return type is based on the custom field type and returns an object that isn't a String.
  • For String comparison, I'd try using the .equals() method instead.

I took a stab at some edits to the script - does this work for you?

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.component.ComponentAccessor

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def type = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Risk Type")).toString()
if (type.equals("New Feature/Function")) {
return "Integrated Testing (manual or auto)"
}
else if (type.equals("Logic change")) {
return "Ad Hoc Testing (manual or auto)"
}
else if (type.equals("UI change")) {
return "Ad Hoc Testing (manual or auto)"
}
else if (type.equals("No functional change")) {
return "No testing required"
}
else {
return ""
}

Suggest an answer

Log in or Sign up to answer