Uploaded image for project: 'SR for Jira - Development'
  1. SR for Jira - Development
  2. SRJIRA-5987

getValue() fetches Date field null value as String

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Triage
    • L3
    • Resolution: Unresolved
    • 6.51.0
    • None
    • Behaviours
    • 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

      1. Create a new Behaviour and map it to all Projects and Issue Types
      2. 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()}")
        }
        
      3. 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

      Attachments

        Activity

          People

            Unassigned Unassigned
            imazuki Irfan Mazuki
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated: