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

Behavior to copy date field to subtask creation form

Peter Garncarek July 14, 2022

I am trying to copy values from parent to subtask creation form and I can't figure out how to handle two date fields.  I am using this as a reference:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.jira.datetime.DateTimeFormatter as JiraDateTimeFormatter
import com.atlassian.jira.issue.customfields.impl.DateTimeCFType
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.user.ApplicationUser
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.transform.BaseScript
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.temporal.TemporalAccessor
import java.time.format.DateTimeFormatter

@BaseScript FieldBehaviours fieldBehaviours

// Set log level
def log = Logger.getLogger(getClass())
log.setLevel(Level.DEBUG)

// The date & date time field names to set a value
final dateFieldName = "Target start"
final dateTimeFieldName = "Target completion"

// Get the components
def customFieldManager = ComponentAccessor.customFieldManager
def authenticationContext = ComponentAccessor.jiraAuthenticationContext
// Get the Jira date time formatter for the logged in user, so we can get the correct locale and user timezone
def userFormatter = ComponentAccessor.getComponent(JiraDateTimeFormatter).forUser(authenticationContext.loggedInUser)

// Get the date & date time field objects
def dateField = customFieldManager.getCustomFieldObjectsByName(dateFieldName).first()
def dateTimeField = customFieldManager.getCustomFieldObjectsByName(dateTimeFieldName).first()

// Set the wanted dates params for values, format pattern and timezone to interpret the dates in
final timeZoneId = ZoneId.systemDefault()
final dateFormatter = DateTimeFormatter.ofPattern('yyyy/MM/dd')
final dateTimeFormatter = DateTimeFormatter.ofPattern('yyyy/MM/dd HH:mm:ss')
final wantedDateAsText = '2020/11/28'
final wantedDateTimeAsText = '2020/11/01 12:12:00'

// Parse the dates as interpreted in the chosen time zone and convert to the user time zone, to make sure the user sees the date in his time zone
def userTimeZoneId = userFormatter.zone.toZoneId()
def wantedDate = LocalDate.parse(wantedDateAsText, dateFormatter)
    .atStartOfDay(timeZoneId)
    .withZoneSameInstant(userTimeZoneId)
def wantedDateTime = LocalDateTime.parse(wantedDateTimeAsText, dateTimeFormatter)
    .atZone(timeZoneId)
    .withZoneSameInstant(userTimeZoneId)

// Populate the fields
//getFieldByName("Target start").setFormValue(getJiraDateFormattedForUser(wantedDate, dateField, userFormatter))
//getFieldByName("Target completion").setFormValue(getJiraDateFormattedForUser(wantedDateTime, dateTimeField, userFormatter))

/**
 * Get the date or date time values in the format expected by Jira an behaviours for fields
 * @Param inputDate The input date or date time to format
 * @Param sourceField The field object in which the value will be set
 * @Param dateTimeFormatter the Jira's date time formatter to extract the user's locale info
 * @return The formatted date time
 */
static String getJiraDateFormattedForUser(TemporalAccessor inputDate, CustomField sourceField, JiraDateTimeFormatter dateTimeFormatter) {
    // Get the Jira date format property key depending on the field type (date or date time)
    def dateFormatPatternProperty = (sourceField.customFieldType in DateTimeCFType) ? APKeys.JIRA_DATE_TIME_PICKER_JAVA_FORMAT : APKeys.JIRA_DATE_PICKER_JAVA_FORMAT
    // Get the format string, so we can format dates how Jira expects
    def dateFormatPattern = ComponentAccessor.applicationProperties.getDefaultBackedString(dateFormatPatternProperty)
    // Build the formatter object
    def formatterForFieldType = DateTimeFormatter.ofPattern(dateFormatPattern, dateTimeFormatter.locale)
    // Return the formatted date string that can be used to set date or date time fields
    formatterForFieldType.format(inputDate)
}

My 'Target start' and 'Target completion' fields get the values from these lines (as expected so logic works):
final wantedDateAsText = '2020/11/28'
final wantedDateTimeAsText = '2020/11/01 12:12:00'
def wantedDate = LocalDate.parse(wantedDateAsText, dateFormatter)
    .atStartOfDay(timeZoneId)
    .withZoneSameInstant(userTimeZoneId)
def wantedDateTime = LocalDateTime.parse(wantedDateTimeAsText, dateTimeFormatter)
    .atZone(timeZoneId)
    .withZoneSameInstant(userTimeZoneId)
 I can't figure out how to read the parent field date and make it functional for the 'wantedDate' variable.

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events