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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,457,311
Community Members
 
Community Events
176
Community Groups

Behavior to copy date field to subtask creation form

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

Atlassian Community Events