Looking for a date field to be the value of another date field plus 1 year

I have a date picker field. I would like to use scriprunner to help me enable a second field to display the date of the first field plus 1 year. Can you show me how to do that? Thanks

1 answer

Hi Maggie,

I do not know the exact requirements but I suggest to use a scripted field. What this actually does is to create a dependent to another field/s custom field. 

It is important to configure your scripted field right, so in your case will be 

For the searcher use the Date Time Range picker. For the template use Date Time Picker.

And for the inline script

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

Issue issue = issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()

// the dependent custom field
def dateACF = customFieldManager.getCustomFieldObjectByName("Date A")

// get the value of the date A custom field
def dateAValue = issue.getCustomFieldValue(dateACF) as Date

// build the new date
def dateB = Calendar.getInstance()

// copy to the new date the value of the Date A custom field

// add one year
dateB.add(Calendar.YEAR, 1)

return dateB.getTime()

So in the end your scripted field will look like

Screen Shot 2017-01-11 at 22.48.30.png

Hope that helps,

regards, Thanos

That works great! Thanks. Is there a way to display the resulting date without including the time?

I didn't notice that I am getting errors in the logs when I use this script:

2017-06-26 14:16:30,497 http-nio-8080-exec-21 ERROR margaret_stearns 856x3297x1 1b46rt /secure/QuickEditIssue.jspa [c.o.scriptrunner.customfield.GroovyCustomField] Script field failed on issue: TP-18, field: Verification Expire Date (1 year)
at java_util_Calendar$setTime$0.call(Unknown Source)
at Script238.run(Script238.groovy:17)

I figured out my problem. I needed to add some code for the case if the date is null (not set).

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Thursday in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

907 views 5 18
Read article

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