It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How set date in custom field value in Issue Create event Edited

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 Community Leader Dec 20, 2019

Hi aas

Try this:

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

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

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

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you