Using the Behaviours plugin to determine is a date field is NULL and if it is we need to copy the value of an existing date field into it. The below only return a value of object instead of the date value
def OriginalDate = getFieldById("customfield_16324")
def ExpectedDate = getFieldById("customfield_16325")
def ODate = OriginalDate.getValue()
def EDate = ExpectedDate.getValue()
if ( ! EDate ) {
ExpectedDate.setFormValue{ODate}
}
Yes, basically you need to format it properly. It might be better to do it like this, if you have users with different language settings I think you may need to:
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.datetime.DateTimeFormatterFactory import com.atlassian.jira.datetime.DateTimeStyle def dateTimeFormatterFactory = ComponentAccessor.getComponentOfType(DateTimeFormatterFactory) def formatter = dateTimeFormatterFactory.formatter().forLoggedInUser().withStyle(DateTimeStyle.DATE_PICKER) formatter.format(your date)
I switched it to the following and it appears to work correctly even with the static checker showing an error
Cannot find matching method com.atlassian.jira.datetime.DateTimeFormatter#format(java.lang.Object). Please check if the declared type is right and if the method exists. Possible solutions: print(java.io.PrinterWriter), print(java.lang.Object) @ line 10, column 21.
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.datetime.DateTimeFormatterFactory import com.atlassian.jira.datetime.DateTimeStyle def oDate = getFieldById("customfield_16324").getValue() def eDate = getFieldById("customfield_16325").getValue() def dateTimeFormatterFactory = ComponentAccessor.getComponentOfType(DateTimeFormatterFactory) def formatter = dateTimeFormatterFactory.formatter().forLoggedInUser().withStyle(DateTimeStyle.DATE_PICKER) def formatteddate = formatter.format(oDate) if ( ! eDate ) { getFieldById("customfield_16325").setFormValue(formatteddate) } if ( oDate != null) { getFieldById("customfield_16324").setReadOnly(true) } else { getFieldById("customfield_16324").setReadOnly(false) }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you want to remove the STC error you can use:
oDate as Date
Problem is the type checker can only infer it's an Object.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Jeff Turner Hi . . Can this script be used for copying the farthest due date value from one issue type to another ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have figured out how to copy 'Due Date' to 'Original Duedate' (if unset). The Groovy code must not go on the 'Initializer', but rather a 'server-side script' on a Due Date field added to the Behaviour.
/** Copies 'Due Date' to 'Original DueDate' if the latter has no value. */
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.datetime.DateTimeFormatterFactory
import com.atlassian.jira.datetime.DateTimeStyle
def dateTimeFormatterFactory = ComponentAccessor.getComponentOfType(DateTimeFormatterFactory)
def formatter = dateTimeFormatterFactory.formatter().forLoggedInUser().withStyle(DateTimeStyle.DATE_PICKER)
//Date formatting courtesy of https://community.atlassian.com/t5/Jira-questions/Copy-date-field-if-date-field-is-null/qaq-p/10671
def dueDateField = getFieldById("duedate")
def dueDate = dueDateField?.value as java.util.Date
def origDueDateField = getFieldById("customfield_14000")
def origDueDate = origDueDateField?.value
//log.debug("Now checking if '${dueDateField}' should be copied to/over '${origDueDateField}'")
if ( ! origDueDate ) {
def String dueDateStr = formatter.format(dueDate)
origDueDateField.setFormValue(dueDateStr)
origDueDateField.setHelpText("Previously blank - has been auto-filled in as the new Due Date")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Jeff Turner Hi Jeff, do you think the above script could be used for picking the farthest value of 'due date' in an issue type to another field in a different issue type ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
On further testing, the above script gets the date off-by-one if the user's timezone differs from the server's. Updated code fixing that:
/** Copies 'Due Date' to 'Original DueDate' if the latter has no value. */
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.datetime.DateTimeFormatterFactory
import com.atlassian.jira.datetime.DateTimeStyle
def dateTimeFormatterFactory = ComponentAccessor.getComponentOfType(DateTimeFormatterFactory)
def formatter = dateTimeFormatterFactory.formatter().withSystemZone().withStyle(DateTimeStyle.DATE_PICKER)
//Date formatting courtesy of https://community.atlassian.com/t5/Jira-questions/Copy-date-field-if-date-field-is-null/qaq-p/10671
def dueDateField = getFieldById("duedate")
def dueDate = dueDateField?.value as java.util.Date
def origDueDateField = getFieldById("customfield_14000")
def origDueDate = origDueDateField?.value
//log.error("Now checking if '${dueDateField}' should be copied to/over '${origDueDateField}'")
if ( ! origDueDate ) {
def String dueDateStr = formatter.format(dueDate)
origDueDateField.setFormValue(dueDateStr)
origDueDateField.setHelpText("Previously blank - has been auto-filled in as the new Due Date")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Handled issue with the follow
import java.text.SimpleDateFormat
def oDate = getFieldById("customfield_16324").getValue()
def eDate = getFieldById("customfield_16325").getValue()
SimpleDateFormat sdf = new SimpleDateFormat("d/MMM/yy")
def formatNewDate = sdf.format(oDate)
if ( ! eDate ) {
getFieldById("customfield_16325").setFormValue(formatNewDate)
}
if ( oDate != null) {
getFieldById("customfield_16324").setReadOnly(true)
} else {
getFieldById("customfield_16324").setReadOnly(false)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Current logic which gives me the entire date with time. Need to be able to strip the time to only have the d/MMM/yy so that it is in accept the correct value
def oDate = getFieldById("customfield_16324").getValue()
def eDate = getFieldById("customfield_16325").getValue()
if ( ! eDate ) {
getFieldById("customfield_16325").setFormValue(oDate)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.