Details
-
Bug
-
Status: Triage
-
L3
-
Resolution: Unresolved
-
6.51.0
-
None
-
None
-
0.7
Description
When fetching an empty Date field value using Behaviour's getValue(), it will fetch the null value as String. This will lead to problems when explicitly casting it as Date, as String cannot be casted to Date type.
Steps to Reproduce
- Create a new Behaviour and map it to all Projects and Issue Types
- Add a Date field and use the following field server-side script:
def dateFormField = getFieldByName("Date Field") try { Date date = (Date) dateFormField.getValue() dateFormField.clearError() } catch (Exception ex) { dateFormField.setError("${ex.getClass().getName()}<br>${ex.getMessage()}") }
- Edit an Issue that has the Date field without value (null)
Expected Behaviour
The script gets executed without error
Actual Behaviour
Error will be displayed about casting String to Date problem
If we slightly modify the script to not explicitly cast as Date type and check the fetched null value's class, it shows as String:
def dateFormField = getFieldByName("Date Field") try { def date = dateFormField.getValue() log.warn("date.class: "+date.class) dateFormField.clearError() } catch (Exception ex) { dateFormField.setError("${ex.getClass().getName()}<br>${ex.getMessage()}") }
Workaround
Fetch the Date field value using generic Object type and evaluate its class accordingly:
def dateFormField = getFieldByName("Date Field") try { Object value = dateFormField.getValue() Date date = value.getClass().equals(Date) ? (Date) value : null dateFormField.clearError() } catch (Exception ex) { dateFormField.setError("${ex.getClass().getName()}<br>${ex.getMessage()}") }
Note:
The problem is not occurring when fetching the Date field null value using getCustomFieldValue() and casting it as Date type
e.g.:
import com.atlassian.jira.component.ComponentAccessor def issue = ComponentAccessor.issueManager.getIssueByCurrentKey("QQQ-1130") def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Date Field")[0] Date date = (Date) issue.getCustomFieldValue(cf)
Run in Script Console