Use Scriptrunner Post-function to set custom time value within custom Date Time Picker field

Michael
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.
February 9, 2024

Hi everyone,

We have a Date Time Picker field (Called "Team Due Date") within our Create screen, which asks the users to input a custom "team" due date.

Within a post function, we would like to adjust JUST the time of this "Team Due Date" field to be the EOD (end of business day) for the due date selected.

Is it possible to do this?

For Example: A user inputs 3/3/2024 00:00:00am -> We would like the post function to change just the time to be our EST end of day, meaning: "3/3/2024 17:00:00pm".

Thanks,

Mike

2 answers

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 10, 2024

Hi @Michael

This is very much doable with ScriptRunner, and it's even simpler when using ScriptRunner's HAPI feature.

Below is an example working code for your reference:-

import groovy.time.TimeCategory
import java.sql.Timestamp

def currentDueDate = issue.getCustomFieldValue('Time Due Date') as Date
def endOfDay = new Date()

use( TimeCategory ) {
endOfDay = currentDueDate + 17.hours
}

issue.set {
setCustomFieldValue('Time Due Date', new Timestamp(endOfDay.time))
}

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the Post-Function configuration:-

post_function_config.png

If you observe the screenshot above, the ScriptRunner editor is complaining about a TypeScript check. You can ignore it, as the code works just fine.

Below are a couple of test screenshots for your reference:-

1. When the issue is first created, the date is set to 15 February 2024 00:00:00, but the format displayed in this instance is 15/Feb/24 12:00 AM.

test1.png

2. Once the issue transitions to In Progress, as expected, the date time field is updated to 15 February 2024 17:00. The format displayed is 15/Feb/24 5:00 PM.

test2.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

Michael
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.
February 16, 2024

Hello again @Ram Kumar Aravindakshan _Adaptavist_

I've just tried your code above during the "create" transition, and we were able to get it to work; but it only adds 17 hours to the currently input time. 

By default, Jira selects the current "time", so none of these scripted tickets will have their time set to 5pm (17:00) unless someone enters a ticket 17hrs before 5pm.

Is it possible to update this code so that the time is set to exactly 5pm regardless of what the user enters?

Thanks,

Mike

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 16, 2024

Hi @Michael

It is possible, but I need to play around with the code.

I'll get back to you once I have the update.

Thank you and Kind regards,

Ram

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 17, 2024

Hi @Michael

Below is the updated working code that I have tested with:

import java.sql.Timestamp
import java.text.SimpleDateFormat

def currentDueDate = issue.getCustomFieldValue('Time Due Date') as String

def format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
def calendar = new GregorianCalendar()
def dateToFormat = format.parse(currentDueDate)
calendar.setTime(dateToFormat)

def currentTime = dateToFormat.toLocalTime()
def hoursDifference = 0
def minutesDifference = 0
def secondsDifference = 0

if (currentTime.hour < 17) {
hoursDifference = 17 - currentTime.hour
minutesDifference = currentTime.minute * -1
secondsDifference = currentTime.second * -1
} else if (currentTime.hour == 17) {
hoursDifference = 0
minutesDifference = currentTime.minute * -1
secondsDifference = currentTime.second * -1
} else if (currentTime.hour > 17) {
hoursDifference = (currentTime.hour - 17) * -1
minutesDifference = currentTime.minute * -1
secondsDifference = currentTime.second * -1
}

calendar.add(Calendar.HOUR, hoursDifference)
calendar.add(Calendar.MINUTE, minutesDifference)
calendar.add(Calendar.SECOND, secondsDifference)

issue.set {
setCustomFieldValue('Time Due Date', new Timestamp(calendar.time.time))
}

With the updated code, irrespective of the time that has been entered into the field, it will automatically reset to 17:00, i.e. 5:00pm

If you observe the screenshots below, irrespective of the time I set in the Time Due Date field, i.e. Date Time field, once the issue is created, it is automatically set to 5:00 p.m.

test1.png

test2.png

 

test3.png

test4.png

test5.png

test6.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram

Michael
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.
February 20, 2024

Hi again @Ram Kumar Aravindakshan _Adaptavist_

I've tried out the code above; customizing the needed parts to correspond with our current "Team Due Date" field as follows:

import java.sql.Timestamp
import java.text.SimpleDateFormat

def currentDueDate = issue.getCustomFieldValue(customfield_20000 'Team Due Date') as String

def format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
def calendar = new GregorianCalendar()
def dateToFormat = format.parse(currentDueDate)
calendar.setTime(dateToFormat)

def currentTime = dateToFormat.toLocalTime()
def hoursDifference = 0
def minutesDifference = 0
def secondsDifference = 0

if (currentTime.hour < 17) {
hoursDifference = 17 - currentTime.hour
minutesDifference = currentTime.minute * -1
secondsDifference = currentTime.second * -1
} else if (currentTime.hour == 17) {
hoursDifference = 0
minutesDifference = currentTime.minute * -1
secondsDifference = currentTime.second * -1
} else if (currentTime.hour > 17) {
hoursDifference = (currentTime.hour - 17) * -1
minutesDifference = currentTime.minute * -1
secondsDifference = currentTime.second * -1
}

calendar.add(Calendar.HOUR, hoursDifference)
calendar.add(Calendar.MINUTE, minutesDifference)
calendar.add(Calendar.SECOND, secondsDifference)

issue.set {
setCustomFieldValue('Team Due Date', new Timestamp(calendar.time.time))
}

But for some reason, the post-function is not changing the time as I've seen in your screenshots:no time change.jpg

I picked the future date (2/23) and kept the time as "default" which Jira selected as the current EST time of 11:39am.

We are currently on the latest LTS 9.12 (data center) Jira version, and I'm also putting this post-function after creation of the issue.

Can you please let me know if I didn't change something within the code that I needed to?

Thanks,

Mike

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 20, 2024

Hi @Michael

There seems to be an error in your code. You have declared your Team Due Date field as such:

def currentDueDate = issue.getCustomFieldValue(customfield_20000 'Team Due Date') as String

You should instead declare it as:-

def currentDueDate = issue.getCustomFieldValue('Team Due Date') as String

I am aware when using the ScriptRunner Post-Function editor, it will automatically display the custom field Id along with the Field name that you have provided.

But please double-check if it has been hard coded. I'm asking this because, even though the field ID is displayed in the ScriptRunner Editor, it will not be included when you copy the code and paste it to a text file or in the comment field.

Also, from the screenshot you have shared, the field is Internal Due Date, as shown in the screenshot below:-

wrong_field_name.png

Please confirm if you have included the Team Due Date in your project. I am asking this because I can't see that field in the screenshot you shared above.

I can only see the Internal Due Date field. If the Team Due Date exists on the project you are testing, please confirm if you have added any value to it when testing.

It would be best to share a screenshot of your create screen.

Thank you and Kind regards,

Ram

 

Michael
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.
February 21, 2024

Hi again @Ram Kumar Aravindakshan _Adaptavist_

I was only displaying the customfield ID that is shown within the editor. I did not hardcode that number into the code. (I just wanted you to see that ScriptRunner was able to find the needed customfield)

As for the name of the customfield, "Internal Due Date" is the actual name of the field and its customfield ID is the displayed "customfield_20000", I was using "team due date" as a non-existent "example" field because it was easier to type within my questions above and I wanted to stay consistent - but I realize that doesn't help troubleshoot issues. Sorry for any confusion.

So with the above, the actual code is as follows:

import java.sql.Timestamp
import java.text.SimpleDateFormat

def currentDueDate = issue.getCustomFieldValue('Internal Due Date') as String

def
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
def calendar = new GregorianCalendar()
def dateToFormat = format.parse(currentDueDate)
calendar.setTime(dateToFormat)

def currentTime = dateToFormat.toLocalTime()
def hoursDifference = 0
def minutesDifference = 0
def secondsDifference = 0

if (currentTime.hour < 17) {
    hoursDifference = 17 - currentTime.hour
    minutesDifference = currentTime.minute * -1
    secondsDifference = currentTime.second * -1
} else if (currentTime.hour == 17) {
    hoursDifference = 0
    minutesDifference = currentTime.minute * -1
    secondsDifference = currentTime.second * -1
} else if (currentTime.hour > 17) {
    hoursDifference =  (currentTime.hour - 17) * -1
    minutesDifference = currentTime.minute * -1
    secondsDifference = currentTime.second * -1
}

calendar.add(Calendar.HOUR, hoursDifference)
calendar.add(Calendar.MINUTE, minutesDifference)
calendar.add(Calendar.SECOND, secondsDifference)

issue.set {
    setCustomFieldValue('Internal Due Date', new Timestamp(calendar.time.time))
} 

Would the position of the post-function have anything to do with the issue? Or maybe the Jira version? We have added this as second post-function right after the "Create Issue" post-function. We are also running the latest Data Center LTS release (9.12.4)

Thanks,

Mike

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 21, 2024

Hi @Michael

Can you place this Post-Function at the very top of your Post-Function configuration and retest.

When using ScriptRunner's HAPI feature, you don't need to place it at the bottom, i.e. after the Create Post-Function.

Thank you and Kind regards,

Ram

Michael
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.
February 21, 2024

Hi again @Ram Kumar Aravindakshan _Adaptavist_

I changed the post-function to be the very first one that runs, and it... kind of worked. It was changing the time to noon est. (12pm est)

I'm assuming it's because the code was pulling of our server time; so I changed "17" to "22" (basically increasing the time by 5 hours so that the time written to the issue would be 5pm est instead of 12pm est - and it now works flawlessly.

Thanks for all the help Ram!

~Mike

0 votes
Fadoua
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 9, 2024

@Michael 

My guessing is yes 

You get your custom field value 

then apply a method that will increase the time to desired timing https://www.tutorialspoint.com/groovy/groovy_dates_times.htm

I can try it if you are willing to wait until tomorrow.

Best,

Fadoua

Michael
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.
February 16, 2024

Hello @Fadoua

We are looking to "overwrite" the time that the user inputs to a specific time instead of just adding time.

The times at which a user will input a request will be dynamic, so we'd need a dynamic solution, or just the ability to "overwrite" the time entered to be "17:00est" (5pm est) which is the end of business day. (EOBD)

Do you have any ideas for this?

Thanks,

Mike

 

Suggest an answer

Log in or Sign up to answer