Using ScriptRunner Post Function to set issue End Date to endOfMonth

Jörg Friedmann September 13, 2018

 Hi, I´m trying to set up a post function script where the Enddate will be set to the end of the current month.

 

The actual script considers the currentdate value +30days.

Has anyone got an idea to change the +30 to endOfMonth?

 

import com.atlassian.jira.component.ComponentAccessor

import java.sql.Date

import java.sql.Timestamp

import com.atlassian.jira.ComponentManager

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.fields.CustomField

import org.apache.log4j.Logger

import org.apache.log4j.Level

 

// Manager

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

 

//Values

CustomField currentDate = customFieldManager.getCustomFieldObject("customfield_11808")

Timestamp currentDateValue = (Timestamp)issue.getCustomFieldValue(currentDate)

 

 

Date newDateValue = new Date(currentDateValue.getTime())

 

//Add 1 month

newDateValue.setMonth(currentDateValue.getMonth() +1)

 

//Update custom field

issue.setCustomFieldValue(currentDate, newDateValue.toTimestamp())

1 answer

0 votes
Orkun Gedik
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 14, 2018

Hello @Jörg Friedmann,

You can use the following code to get end of the month. Just modify to parameters according to your currentDateValue variable.

 GregorianCalendar calendar = new GregorianCalendar(2018, 8, 0);
Date date = calendar.getTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

// dateFormat.format(date) returns the last day of the month
Jörg Friedmann September 14, 2018

Hi Thanks a lot but I´m running on an error

2018-09-14 10_12_21-Update Workflow Function Parameters.png

Can ypu please explain what you mean with currentDateValue vaiable?

I changed the date format to dd-MM-yy also but without success

Orkun Gedik
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 14, 2018

If you want to set all dates to the current month's last day, you don't need use currentDateValue. I missunderstood, forget what I said:) 

By the way did you import the date libraries?

Jörg Friedmann September 14, 2018

I guess not. 

I tried some modifcations but failed, Im not able to adapt your code to mine.

ALso what I do not understand is how your code interacts with the condition to set the new date of the issue to 1month in future.

 

Maybe my explanation was bad. Background is I have a workflow where every issue set to done will be cloned to a new issue. This new issue shall have the end date value changed 1month forward automatically.

Suggest an answer

Log in or Sign up to answer