Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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
dateB.setTime(dateAValue)

// 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 10.56.40.1 /secure/QuickEditIssue.jspa [c.o.scriptrunner.customfield.GroovyCustomField] Script field failed on issue: TP-18, field: Verification Expire Date (1 year)
java.lang.NullPointerException
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).

@Maggie Stearns

 

Can you share the code you use, I am getting null everytime I try to run the script. 

 

Thanks
Ankit

Sure:

 

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 of Most Recent Training')

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

// If the date field is empty ignore it
if (dateAValue==null) {
return null
}

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

// copy to the new date the value of the Date A custom field
dateB.setTime(dateAValue)

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

return dateB.getTime()

Thank you so much, I am new to the Scriptrunner and groovy so appreciate you sharing this. Thank you Maggie!

@Thanos Batagiannis [Adaptavist] Hi. I have a similar requirement for setting up a field to fetch in date value from another field. I tried your script but unfortunately getting the below error,

 "java.lang.NullPointerException"

Can you please help me out here. Thanks 

@Aisha M Can you provide details on what are you setting up and if you just want to copy the date from one field to another? 

 

Thanks

Ankit

@Ankit Patel Unfortunately, my requirement has changed. I want to have a editable field (Date A) which, if left empty should copy a date value from another field (Date B). Also, the user should be able to even select the date on this field (Data A) if needed. The above 'copying logic' must work only if the field (Date A) is left blank.

Hi Thanos - The script you have provided works great for me. I was looking to do an additional enhancement where I will copy custom fields to the Scripted field based on issue type. Eg:

For Issue Type = X, I need to copy the value of custom field X1 to the Scripted Field

For Issue Type = Y, I need to copy the value of custom field Y1 to the Scripted Field

 

Is this possible via a scripted field and if so, what would be your recommendation?

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

JSM June Challenge #2: Share how your business teams became ITSM rockstars

For JSM June Challenge #2, share how your non-technical teams like HR, legal, marketing, finance, and beyond started using Jira Service Management! Tell us: Did they ask to start using it or...

194 views 6 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you