Hello,
I'm quite new in Scriptrunner, but was trying to script custom field, which shows the duration from issue creation to status 'resolved'.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def createDate = issue.getCreated().getTime();
def resolutionDate = issue.getResolutionDate().getTime();
if (resolutionDate == null) {
return null;
}
return (resolutionDate - createDate)/1000 as long;
(Searcher: Duration Searcher
Template: Duration)
Actually the output is correct (see attachment):

but logs sometimes show:
2018-03-30 10:19:57,121 ERROR [customfield.GroovyCustomField]: **********
java.lang.NullPointerException: Cannot invoke method getTime() on null object
at Script952.run(Script952.groovy:7)
It's obvious that this error appears only if you're working in open (not resolved) issues.
How the logic should be modified so that the field only "work for" resolved issues?