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

3 answers

1 accepted

0 votes

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.

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

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

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,760 views 11 18
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot