Trying to add one year to a custom field value

Tom 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
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 28, 2019

Hi @Tom 

You can simply add 365 days as below

def dateB = dateAValue.plus(365)
Tom 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! 

Like Tuncay Senturk likes this
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 29, 2019

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

0 votes
Tom 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
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 28, 2019

Or in your code you should convert GregorianCalendar to date

issue.setCustomFieldValue(dateACF,dateB.toInstant()) 
Tom 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
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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