Copy date field if date field is null

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

This widget could not be displayed.

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

 

 

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.

This widget could not be displayed.

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

This widget could not be displayed.

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

This widget could not be displayed.

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

 

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

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")
}
This widget could not be displayed.

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

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Aug 06, 2018 in Jira Service Desk

A is for Activate: Share your top Jira Service Desk onboarding tips for new users!

Hi, everyone! Molly here from the Jira Service Desk Product Marketing Team :).  In the spirit of this month's  august-challenge, we're sourcing stories of Jira Service Desk activation fro...

536 views 23 15
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you