set date time custom field to current date time using groovy

Paresh Gandhi
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.
May 13, 2014

I hvae code which works fine to modify custom field value if it is text. I want to update the datetime custom field. I guess date time formatting is wrong in my code.

import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import org.apache.log4j.Logger
import static org.apache.log4j.Level.DEBUG
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.TransitionValidationResult
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.util.ErrorCollection
import com.atlassian.jira.issue.index.IssueIndexManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import java.text.SimpleDateFormat
import java.sql.Timestamp
import java.text.DateFormat
import java.util.Date

class SummaryStatusDate extends AbstractIssueEventListener {
Logger log = Logger.getLogger(SummaryStatusDate.class)
@Override
void workflowEvent(IssueEvent event) {
log.setLevel(org.apache.log4j.Level.DEBUG)
def field = event.getChangeLog().getRelated('ChildChangeItem').any{ it.field.toString().equalsIgnoreCase("Summary Updated")}
if (field){
MutableIssue issue = event.issue
ComponentManager componentManager = ComponentManager.getInstance()
def customFieldManager = componentManager.getCustomFieldManager()
IssueManager issueManager = componentManager.getIssueManager()
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Summary Update Date"}
log.debug "Field: " + cf
//def today = new Date()
//log.debug today
//log.debug (new Date().parse("dd MMM YY H:m", new Date()))
//issue.setCustomFieldValue(cfg, "abc")
//issue.store()

IssueChangeHolder changeHolder = new DefaultIssueChangeHolder()
DateFormat.getInstance().format(now)),changeHolder);
Date d2 = new SimpleDateFormat("yyyy-MM-dd HH:MM").parse("2014-05-14 11:50")
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), d2),changeHolder)
}
}
}

5 answers

1 accepted

1 vote
Answer accepted
Paresh Gandhi
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.
May 15, 2014

import com.opensymphony.workflow.WorkflowContext

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.ComponentManager

import com.atlassian.jira.event.issue.AbstractIssueEventListener

import com.atlassian.jira.event.issue.IssueEvent

import org.apache.log4j.Logger

import static org.apache.log4j.Level.DEBUG

import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult

import com.atlassian.jira.bc.issue.IssueService.IssueResult

import com.atlassian.jira.bc.issue.IssueService

import com.atlassian.jira.bc.issue.IssueService.TransitionValidationResult

import com.atlassian.jira.issue.IssueInputParametersImpl

import com.atlassian.jira.issue.IssueInputParameters

import com.atlassian.jira.util.ErrorCollection

import com.atlassian.jira.issue.index.IssueIndexManager

import java.sql.Timestamp

import com.atlassian.jira.issue.comments.CommentManager

import com.atlassian.crowd.embedded.api.User

import com.atlassian.jira.component.ComponentAccessor

import com.opensymphony.workflow.WorkflowContext

import com.atlassian.jira.user.ApplicationUser

import com.atlassian.jira.issue.MutableIssue

import com.atlassian.jira.issue.IssueManager

import com.atlassian.jira.event.type.EventDispatchOption

class SummaryStatusDate extends AbstractIssueEventListener {

Logger log = Logger.getLogger(SummaryStatusDate.class)

@Override

void workflowEvent(IssueEvent event) {

log.setLevel(org.apache.log4j.Level.DEBUG)

def field = event.getChangeLog().getRelated('ChildChangeItem').any{ it.field.toString().equalsIgnoreCase("Summary Updated")}

if (field){

MutableIssue issue = event.issue

ComponentManager componentManager = ComponentManager.getInstance()

def customFieldManager = componentManager.getCustomFieldManager()

IssueManager issueManager = componentManager.getIssueManager()

def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Summary Update Date"}

def date1 = new Timestamp(new Date().getTime())

issue.setCustomFieldValue(cf, date1)

User currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

issueManager.updateIssue(currentUserObj, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

issue.store()

}

}

}

2 votes
Shawn White March 17, 2015

Lots posts on this board asking the same question but I found nothing that works. I used to do tons of Java script, but not for years, and I'm just getting started with Groovy scripts and JIRA config. I beat my head against this problem for hours on end.

Here is something that works for me in a WF transition script:

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import static java.lang.Math.*
import java.sql.Timestamp
 
//Get current issue
Issue issue  		= issue
customFieldManager 	= ComponentManager.getInstance().getCustomFieldManager()

//Getting custom field value
cfSSDate        	= issue.getCustomFieldValue( customFieldManager.getCustomFieldObjectByName("Date of Site Survey") )
 
//Getting custom field object
cfoSID        		= customFieldManager.getCustomFieldObjectByName("Scheduled Install Date")

//change Scheduled Install Date to Date of Site Survey + 7 days (604800000ms)
dSID =  new java.sql.Timestamp(cfSSDate.getTime() + 604800000)


//pass setCustomFieldValue a custom field object and a java.sql.Timestamp
issue.setCustomFieldValue(cfoSID, dSID)
 
//this works too today = new java.sql.Timestamp(new Date(2015,5,17).getTime())
//issue.setCustomFieldValue(cfoSID, today)
0 votes
Lauren Robinette December 20, 2018

Hi there! I'm trying to use this script to set a timestamp in a custom field (Time Stamp In Progress) in a post-function transition. I need to set Time Stamp In Progress to the current date and time when the transition from Open to In Progress occurs. I'm getting multiple errors:

On line 10: [Static type checking] - cannot find matching method com.atlassian.jira.ComponentManager#getCustomerFieldManager(). Please check if the declared type is correct and if the method exists.

On line 13: [Static type checking] - the variable [cfSSDate] is undeclared.

On line 16: [Static type checking] - the variable [cfoSID] is undeclared.

On line 19: [Static type checking] - the variable [date] is undeclared. Cannot find matching method com.atlassian.jira.issue.MutableIssue#setCustomFieldValue(java.lang.Object). Please check if the declared type is correct and if the method exists.

 

I don't have much experience with Groovy Scripts, so any advice is appreciated!

groovy script_12.20.JPG

0 votes
JamieA
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.
May 13, 2014

Your date format looks wrong, you have MM twice. I think you want a java.sql.Timestamp, not a Date.

Paresh Gandhi
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.
May 13, 2014

can you please provide me an example

JamieA
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.
May 14, 2014
Paresh Gandhi
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.
May 14, 2014

Thanks!

I was able to set time using

issue.setCustomFieldValue(cf, new Timestamp(new Date(2012,9,17).getTime()))
however how do i set it to current date and time. I counld not find function to pass that to above new Timestamp method.
JamieA
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.
May 14, 2014

new Timestamp(new Date()) or something.

Paresh Gandhi
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.
May 14, 2014

i tried couple of options but those did not work :(

j2166730 August 20, 2018

date.toTimestamp()

0 votes
Paresh Gandhi
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.
May 13, 2014

I also tried

cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), new java.util.Date()),changeHolder);

but did not work

it throws

org.ofbiz.core.entity.GenericEntityException: while inserting: [GenericEntity:CustomFieldValue][id,10270][datevalue,Wed May 14 12:16:21 CDT 2014][issue,10119][parentkey,null][customfield,10009] (Java type java.util.Date not currently supported. Sorry.)

Suggest an answer

Log in or Sign up to answer