Scripted Field: Return specific phrases based on custom field values

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
Accepted answer

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

 

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.

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

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

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
Community showcase
Posted Thursday in Jira

Continuous visibility with the new Jira Software Cloud and Bitbucket Cloud integration

Hey Atlassian community, I am a PM on the Bitbucket Cloud team and I am excited to share that we just released an improvement to the Jira Software and Bitbucket Cloud integration. With the newly im...

76 views 0 3
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