Hi,
I am getting a strange error when calling getCustomFieldValue() on a scripted field that has not been shown on a screen yet. I am running the following code in the Scriptrunner console:
<imports ad lib...>
void logCustomField(CustomFieldManager customFieldManager, Issue issue, String fieldName) {
CustomField[] customFields = customFieldManager.getCustomFieldObjectsByName(fieldName)
customFields.each{ CustomField customField ->
log.error(customField.getName() + " = " + issue.getCustomFieldValue(customField))
}
}
CustomFieldManager customFieldManager = ComponentAccessor.getComponent(CustomFieldManager)
IssueManager issueManager = ComponentAccessor.getIssueManager()
Issue issue = issueManager.getIssueObject("<some issue key>")
logCustomField(customFieldManager, issue, "Work Email") // Single line text field
logCustomField(customFieldManager, issue, "Resolver Group") // Single select drop-down
logCustomField(customFieldManager, issue, "Calculated Change Risk") // Calculated field
log.error("Done")
The output I get is:
2019-07-25 09:58:35,186 ERROR [runner.AbstractScriptRunner]: Work Email = email
2019-07-25 09:58:35,186 ERROR [runner.AbstractScriptRunner]: Resolver Group = null
By adding some more log statements I have pinpointed the problem to the getCustomFieldValue() call for the calculated field. And the code just stops. I have tried to enclose the call with a try-catch, the catch part is never reached.
But, the error only occurs if the calculated field has not been shown on a screen yet. If I view '<some issue key>' on a screen with the calculated field and try to run the code again, it works.
Using the preview function of scripted fields does not change anything - it still depends on the field having been shown on a screen.
I realise that it probably has something to do with the field not having been calculated and/or indexed yet. For my use case, it is okay that the return value is null or an exception is thrown - as long as my code is not exited.
And one final thing: When moving issues between projects in the GUI it seems that not all fields are listed - I cannot say for certain that it is the same problem but I have a bad feeling about this...
We are at Jira Core 8.1.0 / Scriptrunner 5.5.9.1-jira8
Best regards, Sune