Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×We have two custom date/time fields Incident Start Date/Time and Restored Date/Time. I would like to use a scripted field that will give me the time difference between the two. I am unfamiliar with groovy scripts. Where would be a good place to start to create this script? Is there anyway to reach out to scriptrunner directly to help me write the script?
Thank you
Hi Brooke,
The following should get you pretty close. You can test it in Scriptrunner's Script Console by uncommenting line 4 and passing it a valid Issue Key.
import com.atlassian.jira.component.ComponentAccessor
// Tip: Use the following line for debugging in Script Console
// def issue = ComponentAccessor.getIssueManager().getIssueObject("PRJ-123")
def date1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Start Date/Time")?.getValue(issue)
def date2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Restored Date/Time")?.getValue(issue)
if (date1 && date2) {
use(groovy.time.TimeCategory) {
def duration = date2 - date1
return "${duration.days} days ${duration.hours} hours ${duration.minutes} minutes"
}
}
Hope this helps,
Bryan
Thank you for your help! One error I am getting is about categories on line 10
Error: [static type checking] - Due to their dynamic nature, usage of categories is not possible with static type checking active
Any suggestions?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That's just ScriptRunner telling you that it can't validate the script because of the use(groovy.time.TimeCategory) function. The code should still return the value you want.
More info: https://scriptrunner.adaptavist.com/latest/jira/#_static_type_checking
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.