Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Trying to add one year to a custom field value

Tom
Contributor
October 28, 2019

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
Tuncay Senturk _Snapbytes_
Community Champion
October 28, 2019

Hi @Tom 

You can simply add 365 days as below

def dateB = dateAValue.plus(365)
Tom
Contributor
October 29, 2019

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! 

Tuncay Senturk _Snapbytes_
Community Champion
October 29, 2019

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

0 votes
Tom
Contributor
October 29, 2019

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)

0 votes
Tuncay Senturk _Snapbytes_
Community Champion
October 28, 2019

Or in your code you should convert GregorianCalendar to date

issue.setCustomFieldValue(dateACF,dateB.toInstant()) 
Tom
Contributor
October 29, 2019

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

Tuncay Senturk _Snapbytes_
Community Champion
October 29, 2019

My bad, I think this should work

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

Suggest an answer

Log in or Sign up to answer