set date time custom field to current date time using groovy

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)
}
}
}

4 answers

1 accepted

0 votes
Accepted answer

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()

}

}

}

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)

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.)

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

can you please provide me an example

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.

new Timestamp(new Date()) or something.

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

j2166730 I'm New Here Aug 20, 2018

date.toTimestamp()

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,728 views 17 21
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you