Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Error calling getCustomFieldValue() on scripted fields

Sune Vester Lindhe
Contributor
July 25, 2019 edited

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

 

2 answers

1 vote
Sune Vester Lindhe
Contributor
August 19, 2019

A little update. Talked to Adaptivist support, they have now logged this a bug: https://productsupport.adaptavist.com/browse/SRJIRA-3741

0 votes
Ilya Turov
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.
July 25, 2019

I feel like it's not about field not being calculated/indexed, but about 

customFieldManager.getCustomFieldObjectsByName("Calculated Change Risk")

not finding any field at all. are you sure field with this name exists?

Sune Vester Lindhe
Contributor
July 25, 2019

Hi Ilya,

Thanks for the response.

 

The field does exist. I added the last line

log.error("Done")

To demonstrate that the script stopped while trying to print of information regarding the scripted field.

Also, I have changed the line logging the field value to:

log.error(customField.getName() + " = " + (customField.getCustomFieldType().getName() != "Scripted Field" ? issue.getCustomFieldValue(customField) : "<scripted field>"))

And I do get the expected output:

Calculated Change Risk = <scripted field>

Suggest an answer

Log in or Sign up to answer