Forums

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

To update a date field using script runner

Ilakiya Srinivasan
Contributor
June 25, 2021

Hi, 

I want to update a Date custom field by adding 90 days to the value of another date custom field. Say, Date1 is today, I want the Date2 field to have the date 90days from today, updated automatically upon a transition using script runner. Below is the script I used, the script ran successfully, but the field value didn't get updated in the ticket. I'm not sure what I have missed. Could you please help me with this. 

 

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('Date1')
def dateB = customFieldManager.getCustomFieldObjectByName("Date2")

// 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
dateB = Calendar.getInstance()

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

dateB.add(Calendar.MONTH, 3)

return dateB.getTime()

1 answer

1 accepted

0 votes
Answer accepted
Martin Bayer _MoroSystems_ s_r_o__
Community Champion
June 25, 2021

Hi @Ilakiya Srinivasan , you need to update custom field on issue. Your postfunction only returns dateB.getTime() but you're not setting the new date to the field. There is nice example of how to update custom field in postfunction here:

https://library.adaptavist.com/entity/change-the-value-of-a-custom-field-in-a-post-function

Let me know if it helps or you need more help ;)

Ilakiya Srinivasan
Contributor
June 27, 2021

Thanks @Martin Bayer _MoroSystems_ s_r_o__, it really helps!

However, I actually used the JMWE post function 'Set the value of a field to a constant value or the result of a Groovy expression or Groovy Template' to meet my requirement.

Suggest an answer

Log in or Sign up to answer