Hello ScriptRunner enthusiasts,
We've developed a ScriptRunner scripted field called "In Epic" that is supposed to return the Epic Name of a Story or Sub-Task.
For example if Epic ABC-123 had a Story ABC-456 the 'In Epic" field would return in a string format the Epic Name (a field in Jira). Our users like to remember Epic names, not Epic keys.
In our log file we get this error.
2020-06-21 20:10:34,777+0000 IssueIndexer:thread-16 ERROR ub57988 1186x56429x2 1tazjek 204.138.240.254,10.12.0.237 /secure/admin/IndexReIndex!reindex.jspa [c.o.scriptrunner.customfield.GroovyCustomField] *************************************************************************************
2020-06-21 20:10:34,779+0000 IssueIndexer:thread-16 ERROR ub57988 1186x56429x2 1tazjek 204.138.240.254,10.12.0.237 /secure/admin/IndexReIndex!reindex.jspa [c.o.scriptrunner.customfield.GroovyCustomField] Script field failed on issue: CBTGRV-1371, field: In Epic
java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object
at Script186.run(Script186.groovy:36)
-----------------------------------------------------------------------------------------------------
Here is the code itself
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue
def issueManager = ComponentAccessor.getIssueManager() //Get Issue Manager
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager() //Get Custom Field Manager
def cfIssueType = issue.issueType.getName() //Get issue type name in string format
def cfIssueKey = issue.key //Get issue key in string format
def epicLink = customFieldManager.getCustomFieldObjectsByName('Epic Link')[0] //Get Epic Link field object
def epicName = customFieldManager.getCustomFieldObjectsByName('Epic Name')[0] //Get Epic Name field object
log.warn('Issue type: ' + cfIssueType)
switch(cfIssueType) {
case ['Story']:
def epicKey = issue?.getCustomFieldValue(epicLink).toString() //Get Epic link field value
if (epicKey) {
def epicIO = issueManager.getIssueObject(epicKey) //Return issue object from EpicLink Key
def epicDisplayName = epicIO.getCustomFieldValue(epicName) //Return Epic Display Name as string
return epicDisplayName
}
break;
case ['Sub-task']:
def parentStory = issue.parentObject //Define parentStory as issue paren object
def epicKey = parentStory?.getCustomFieldValue(epicLink).toString() //Get Key value of Epic Link as string
if (epicKey) {
def epicIO = issueManager.getIssueObject(epicKey) //Return issue object from EpicLink Key
def epicDisplayName = epicIO.getCustomFieldValue(epicName) //Return Epic Display Name as string
return epicDisplayName
}
break;
default:
return
break;
}
We want to figure out how to deal with this null object error that the log file is reporting.
sample asd code block