How to store datetime via scriptrunner postfunction in JIRA Cloud

Laci Kremer March 12, 2021

Howdy,

 

Using JIRA Cloud, I'm trying to store the current date in a datetime custom field. This has to happen after a transition, if the field is not empty. Solutions I've been exploring:

1. simply add a post-function custom field update, use the %%CURRENT_DATETIME%% , but this does overwrite existing values

2. use the approach above, create two transitions and in the conditions, use a scripted condititon to check if the field is empty, and allow only the transition that is right for the situtation - ugly, as there are a bunch of conditions already and there will be two transitions (this is my backup plan)

3. use Automation - unfortunately there is no conditions that can check datetime customfields

4. write a post-function script using scriptrunner and handle the null check with code

 

I'm at nr. 4, all good, but can't store the time. I'm lost with the dateformat.  

def today = new Date()
def formattedToday = today.format('YYYY-MM-dd hh:mm:ss')

This doesn't work, I get this error:

2021-03-12 15:38:36.933 WARN - PUT request to /rest/api/2/issue/RM-18 returned an error code: status: 400 - Bad Request
body: {"errorMessages":[],"errors":{"customfield_10038":"Error parsing time: 2021-03-12 03:38:36"}}

 

How should the format string look like? 

2 answers

1 accepted

3 votes
Answer accepted
Laci Kremer March 16, 2021

I guess I found the correct format, now it seems to be working:

def today = new Date()
def
formattedToday = today.format("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")

This is going to end up in ISO 8601 format that is consumed by JIRA correctly. 

Found it here: Parse date to “yyyy-MM-dd'T'00:00:00” using Groovy  

0 votes
Kate Kabir
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.
March 16, 2021

Hi @Laci Kremer 

Thank you for your question.

We have an example in our documentation here to refer to as a guide. This documentation, it will show you how to set a date field and the format date needs to be in.

I hope this will help.

Kind Regards

Kate

Laci Kremer March 16, 2021

Hi Kate,

 

Thank you for your response. The linked doc and examples are great, but only shows the date format, not the datetimeThe field I'm working with is a DateTime picker. 

Do you have examples for storing datetime?

 

(I just tried storing the value what I get from new Date(), but that is not being parsed either:

def today = new Date()

put("/rest/api/2/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields:[
(myCustomField): today as String
]
])
.asString() 

 this results in:

2021-03-16 13:45:33.722 WARN - PUT request to /rest/api/2/issue/RM-31 returned an error code: status: 400 - Bad Request
body: {"errorMessages":[],"errors":{"customfield_10038":"Error parsing time: Tue Mar 16 13:45:33 UTC 2021"}}

 

Please advise, how to store time in a customfield with ScriptRunner!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events