Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Set date on a custom field using script runner

Swarna Radha
Contributor
April 12, 2018

Hi,

I want to set date on a custom field using script runner. I am using the script below but it is giving me the date 1 month later not the current date.  I have added the code in a Post Function.

 

code:


def csDate2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Approved On")

Timestamp csDate1Value = (Timestamp) issue.getCreated()
Date csNewDateValue = new Date(csDate1Value.getTime() + 30*24*60*60*1000);
csDate2.updateValue(null, issue, new ModifiedValue("", (Object) csNewDateValue), new DefaultIssueChangeHolder())

 

Kindly advice

Thanks

Swarna

1 answer

1 accepted

3 votes
Answer accepted
Alexey Matveev
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.
April 12, 2018

It gives you one month later, because you add 30*24*60*60*1000, which is 30 days after the current date. You should do it like this:

def csDate2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Approved On")

Timestamp csDate1Value = (Timestamp) issue.getCreated()
Date csNewDateValue = new Date(csDate1Value.getTime());
csDate2.updateValue(null, issue, new ModifiedValue("", (Object) csNewDateValue), new DefaultIssueChangeHolder())
Swarna Radha
Contributor
April 15, 2018

Hi Alexey,

Thanks for your help :)

The code is now working

Thanks 

Swarna

Alexey Matveev
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.
April 15, 2018

If my answer helped you, kindly accept it. In this case other users will be able to  find this answer, if they have similair question.

Lauren Robinette
Contributor
December 20, 2018

Hi @Alexey Matveev, I'm trying to use this script, and am seeing the following errors. The second error says unable to resolve class ModifiedValue @ line 5, column 34 and unable to resolve class DefaultIssueChangeHolder @ line 5, column 82.

Any advice?

jira error_12.20.18.png

Alexey Matveev
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, 2018

You need to add as the first line this line:

import java.sql.Timestamp

Lauren Robinette
Contributor
December 20, 2018

Hi @Alexey Matveev that worked! How about these two errors?

jira error_12.20.18 2.png

Lauren Robinette
Contributor
December 20, 2018 edited

import error.JPG When I type in the import code, I'm getting errors that the system is unable to resolve class

Alexey Matveev
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, 2018

import com.atlassian.jira.issue.ModifiedValue

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

Suggest an answer

Log in or Sign up to answer