Scripted Field: Return specific phrases based on custom field values

Clayton Chancey September 15, 2017

Hi all,

I am trying to use a Scripted Field to return a list of various responses, based on particular field values. For example, if the field "App1 Action Required" has a value of "Yes," I want the phrase "App1 Admin" to be added to a list that returns as the scripted field value. This is the inline script, which is currently returning a java.lang.NullPointerException... help?!

 

//resources
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
def customFieldMgr = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
//define result
def result = []
//add the specific phrase "App1 Admin" to the list of results if the value of c.f. "App1 Action Required" equals "Yes"
def actionApp1 = customFieldMgr.getCustomFieldObjectByName("App1 Action Required")
def valueApp1 = issue.getCustomFieldValue(actionAptos).toString()
if (valueApp1 == "Yes") {
def printApp1 = "App1 Admin"
result.add(printApp1)
}
//add the specific phrase "App2 Admin" to the list of results if the value of c.f. "App2 Action Required" equals "Yes"
def actionApp2 = customFieldMgr.getCustomFieldObjectByName("App2 Action Required")
def valueApp2 = issue.getCustomFieldValue(actionADP).toString()
if (valueApp2 == "Yes") {
def printApp2 = "App2 Admin"
result.add(printApp2)
}
//print full list of results
result

 Disclaimer: I am not very good at this..

1 answer

1 accepted

0 votes
Answer accepted
Joshua Yamdogo @ 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.
September 15, 2017

You have 

def valueApp1 = issue.getCustomFieldValue(actionAptos).toString()

But actionAptos is not defined anywhere in your code? You should be passing in the custom field object that you got the line before ("actionApp1"):

//resources
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
def customFieldMgr = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
//define result
def result = []
//add the specific phrase "App1 Admin" to the list of results if the value of c.f. "App1 Action Required" equals "Yes"
def actionApp1 = customFieldMgr.getCustomFieldObjectByName("App1 Action Required")
def valueApp1 = issue.getCustomFieldValue(actionApp1).toString()
if (valueApp1 == "Yes") {
def printApp1 = "App1 Admin"
 result.add(printApp1)
}
//add the specific phrase "App2 Admin" to the list of results if the value of c.f. "App2 Action Required" equals "Yes"
def actionApp2 = customFieldMgr.getCustomFieldObjectByName("App2 Action Required")
def valueApp2 = issue.getCustomFieldValue(actionApp2).toString()
if (valueApp2 == "Yes") {
def printApp2 = "App2 Admin"
 result.add(printApp2)
}
//print full list of results
result

 

Clayton Chancey September 15, 2017

I forgot to change that one. (I changed the names of the actual "apps" we are using in our instance.) Good catch! However, even when aligning my verbiage as you describe, I am still drawing a java.lang.NullPointerException.

Joshua Yamdogo @ 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.
September 18, 2017

Hi Clayton,

Thanks for the information. The fields you are using are checkbox fields, right? In that case, getting the custom field value of a checkbox field will return an array of values, e.g. if someone selects 'Yes', the value returned will be: [Yes]. You should perhaps try calling the contains() method on the checkbox value instead.

//resources
import com.atlassian.jira.component.ComponentAccessor

def customFieldMgr = ComponentAccessor.getCustomFieldManager()

//define result
def result = []

//add the specific phrase "App1 Admin" to the list of results if the value of c.f. "App1 Action Required" equals "Yes"
def actionApp1 = customFieldMgr.getCustomFieldObjectByName("App1 Action Required")
def valueApp1 = issue.getCustomFieldValue(actionApp1) as String
if (valueApp1.contains("Yes")) {
def printApp1 = "App1 Admin"
  result.add(printApp1)
}

//add the specific phrase "App2 Admin" to the list of results if the value of c.f. "App2 Action Required" equals "Yes"
def actionApp2 = customFieldMgr.getCustomFieldObjectByName("App2 Action Required")
def valueApp2 = issue.getCustomFieldValue(actionApp2).toString()
if (valueApp2.contains("Yes")) {
def printApp2 = "App2 Admin"
  result.add(printApp2)
}
//print full list of results
result
Clayton Chancey September 18, 2017

Thanks for your response, Josh.. I actually got it working using the script below. Thank you for your help!

 

//preparatory
import com.atlassian.jira.component.ComponentAccessor
def customFieldMgr = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def result = []

//build outputs
def app1value = customFieldMgr.getCustomFieldObjectByName("Action Required App1")
def app1print = "App1 Admin"
if (app1value) {
def app1opt = issue.getCustomFieldValue(app1value)
if (app1opt && app1opt.getValue() == "Yes") {
result.add(app1print)}};

def app2value = customFieldMgr.getCustomFieldObjectByName("Action Required App2")
def app2print = "App2 Admin"
if (app2value) {
def app2opt = issue.getCustomFieldValue(app2value)
if (app2opt && app2opt.getValue() == "Yes") {
result.add(app2print)}};

//print output
result
Joshua Yamdogo @ 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.
September 18, 2017

Glad to hear it is working, Clayton. Apologies that I could not get back to you sooner.

Thanks for using ScriptRunner!

Suggest an answer

Log in or Sign up to answer