How set date in custom field value in Issue Create event

aas December 20, 2019

Hello everybody. I try to make Script Listener, which in the time of issue creation set in custom field time which is created time + 2 hours

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import java.sql.Timestamp

def createTime = event.issue.created
log.warn("Created at : " + createTime)

def cfManager = ComponentAccessor.getCustomFieldManager()
def cfField = cfManager.getCustomFieldObject("customfield_34850")
def fieldValue = event.issue.getCustomFieldValue(cfField)


def newTime = new Timestamp(createTime.time)
Long duration = ((120 * 60) + 00) * 1000;
newTime.setTime(newTime.getTime() + duration);

MutableIssue issueToUpdate = (MutableIssue) event.issue
issueToUpdate.setCustomFieldValue(cfField, newTime)
IssueManager issueManager = ComponentAccessor.getIssueManager()
issueManager.updateIssue(event.getUser(), issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false)

 But have error in last line 

java.lang.String cannot be cast to java.util.Date 

Where have I made mistake?

2 answers

1 accepted

0 votes
Answer accepted
Leonard Chew
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.
December 20, 2019

Hi aas

Try this:

def newTime = event.issue.created
def newHours = event.issue.created.getHours() + 2
newTime.setHours(newHours)

It also works if the hours pass midnight (new day is set).

0 votes
aas December 20, 2019

There is no mistake in code. It's just need that custom field has some value by default. Then script works properly.

Suggest an answer

Log in or Sign up to answer