Copy date field if date field is null

Kevin Dalton
Contributor
January 16, 2017

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}
}

5 answers

1 accepted

0 votes
Answer accepted
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 17, 2017

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)
Kevin Dalton
Contributor
January 17, 2017

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)
}

 

 

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 17, 2017

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.

0 votes
Aisha M
Contributor
August 3, 2018

@Jeff Turner Hi . . Can this script be used for copying the farthest due date value from one issue type to another ? 

0 votes
Jeff Turner
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 25, 2018

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")
}

 

Aisha M
Contributor
August 1, 2018

@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 ? 

Jeff Turner August 2, 2018

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")
}
0 votes
Kevin Dalton
Contributor
January 17, 2017

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)
}

0 votes
Kevin Dalton
Contributor
January 17, 2017

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)
}

Suggest an answer

Log in or Sign up to answer