Store current date in post-function?

David Yu
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 25, 2011

Hi, thought I'd ask the community if there was a way to save the current date/time in a post-function before I start treading towards the hard path.

The purpose is to store, and search dates when events have occurred...that is until the feature to search by transition date becomes more robust in JIRA.

1 answer

1 accepted

2 votes
Answer accepted
Betsy Walker
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 26, 2011

Here is some Groovy code that sets a custom field of time Date Picker to the current date. You may need to adjust the SimpleDateFormat so it reflects how you have configured dates in JIRA.

It's currently stripping out the time, but you can edit it to not do that.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.view.CustomFieldParamsImpl
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import org.apache.log4j.Category
import java.sql.Timestamp
import java.text.SimpleDateFormat
import com.opensymphony.user.User

// Some of the code was derived from code shown here:
// http://forums.atlassian.com/thread.jspa?messageID=257317820&#257317820

log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")

// SimpleDateFormat used for debugging
SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy")

CustomFieldManager cfManager = ComponentManager.getInstance().getCustomFieldManager()

MutableIssue myIssue = issue

// Grab current date, and wipe out time component
Calendar cal = Calendar.getInstance()
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.HOUR_OF_DAY,0);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);

// Convert to GregorianCalendar
GregorianCalendar gcal = (GregorianCalendar) cal

// Convert to Timestamp object. It will be stored in the Sign-Off Date field later.
// Sign-Off Date (customfield_10835) is of type DatePicker
CustomField cfSignOffDate = cfManager.getCustomFieldObject("customfield_10835")
Timestamp newDate = new Timestamp(gcal.getTimeInMillis())

myIssue.setCustomFieldValue(cfSignOffDate, newDate)

// If you want to store the date into a text field, such as to maintain a rolling
// history of sign-offs, you can use this code which formats it as a Wiki table:
// 
// CustomField cfRptgHistory = cfManager.getCustomFieldObject("customfield_10825")
// String cfRptgHistoryValue = (String) myIssue.getCustomFieldValue(cfRptgHistory) ?: ""
// 
// cfRptgHistoryValue = '|' + sdf.format(newDate) + '| \n ' + cfRptgHistoryValue
// myIssue.setCustomFieldValue(cfRptgHistory, cfRptgHistoryValue)

issue.store()

David Yu
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 26, 2011

Thanks BetsyW. This will save me a bunch of time!

Suggest an answer

Log in or Sign up to answer