Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

Recognition

  • Give kudos
  • My kudos

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

Trying to add one year to a custom field value

Hi All,

Please can you advise:

I'm trying to create a script runner post-function to add one year to a field.

Have the below,

------------------------------------------------------------

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

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

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

// 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)

//write back to custom field

issue.setCustomFieldValue(dateACF,dateB)

------------------------------------------------------------

This appears to validate OK in the Scriptrunner add on but when i try to execute the workflow transition i get the below error:

"Error occurred while creating issue. This could be due to a plugin being incompatible with this version of JIRA. For more details please consult the logs, and see: http://confluence.atlassian.com/x/3McB java.util.GregorianCalendar cannot be cast to java.util.Date"

Any advice would be appreciated.

Tom

 

 

3 answers

1 accepted

0 votes
Answer accepted

Hi @Tom 

You can simply add 365 days as below

def dateB = dateAValue.plus(365)

Thank you so much Tuncay. You are amazing! I've been struggling with this for a fortnight. 

I can't believe someone on the Alsatian forum answered one of my questions with a succinct, rational answer and not developerspeak. I'm so Happy! 

I'm glad that it helped. Thanks for your kind words.

Or in your code you should convert GregorianCalendar to date

issue.setCustomFieldValue(dateACF,dateB.toInstant()) 

Hi, Sorry tried this one but does not work.

Using the code:

-------------------

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

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

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

// 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)

// convert variable to correct type + write back to custom field
issue.setCustomFieldValue(dateACF,dateB.toInstant())

---------------

Generates the below error on the workflow transition:

http://confluence.atlassian.com/x/3McB java.time.Instant cannot be cast to java.util.Date

My bad, I think this should work

issue.setCustomFieldValue(dateACF, new Date(dateB.getTime()))

For the benefit of anyone else who is struggling with this and trawling the internet with despair;

------------------------------------------

//The below ScriptRunner post-function adds a specific number of days e.g. 365 to a custom field.

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

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

//define the dependent custom field
def dateACF = customFieldManager.getCustomFieldObjectByName("CustomFieldName")

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

// build the new date
def dateB = dateAValue.plus(365)

//write back to custom field
issue.setCustomFieldValue(dateACF,dateB)

Suggest an answer

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

How to create Jira issus from Excel file?

When to use CSV importer When managing your processes in Jira, there are many occasions where you need to create a lot of tasks. Creating them one by one will cost you a lot of time and effort and i...

4,582 views 22 33
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