How can i set a custom field value to current date + some days using post function in jira server

I want to set the valuie of custom field "Remediation Deadline+= Created date +30 days using post function. Can someone please suggest how to go-ahaed i tried with the script runner but the same is not working.

2 answers

1 vote

Hello, 

It would be something like this

import com.atlassian.jira.component.ComponentAccessor
import java.sql.Date
import java.sql.Timestamp
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue



def csDate2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Remediation Deadline")

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

@Alexey Matveev

Hi Alexey,

Getting the below error after running ths script in the script console. Also do let me know how to execute the above script as a psot function .

2017-12-20 05:23:23,225 WARN [common.UserScriptEndpoint]: Script console script failed:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script88.groovy: 13: expecting EOF, found ')' @ line 13, column 113.
ew DefaultIssueChangeHolder()));

Alexey Matveev Community Champion Dec 20, 2017

Script must be like this

import com.atlassian.jira.component.ComponentAccessor
import java.sql.Date
import java.sql.Timestamp
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue



def csDate2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Remediation Deadline")

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

 Go to your workflow and add the Script  Post Function post function. Choose there Custom Script Post-Function and paste the script above.

@Alexey Matveev

Hi Alexey,

I am doing that only but somehow when i pasted the code and click on add its processing only and not going ahead.

The is a field script file , we need to provide which path in this field? pfa screen shot for the same. 

 

 

SCRIPT RUNNER.JPG

Alexey Matveev Community Champion Dec 20, 2017

You do not need to provide the Script File. You should paste the script and wait until the flickering circle becomes red or green. If it is green you can push the add button. 

@Alexey Matveev

The circle is neither turning green nor red

Alexey Matveev Community Champion Dec 20, 2017

Try to use a different browser

@Alexey Matveev

Getting the below error message while running the above script in the post function - please help

Time (on server): Wed Dec 20 2017 08:07:25 GMT-0500 (Eastern Standard Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2017-12-20 08:07:25,062 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2017-12-20 08:07:25,226 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.NullPointerException: Cannot invoke method getTime() on null object
at Script2.run(Script2.groovy:13)

Alexey Matveev Community Champion Dec 20, 2017

That is strange. It means that issue.getCreated() returned no value. Where did you add the post function?

@Alexey Matveev

 

At the create post function. pfb screen shot of the workflow, Once the issue is created i need to autmoatically populate the Remediation deadline date = Created date +30 days.

 

Workflow.PNG

Alexey Matveev Community Champion Dec 20, 2017

You should put your post-function last. It would be much help if you attach a screenshot with all post-functions for the create transition.

@Alexey Matveev

Hi i have changed the position of thw work flow, pfb screen shot, can you let me know how the issue key will be passed? Since it is a post function it will automatically take the Issue key, pfb screen shot -

 

Create post function.PNG

Alexey Matveev Community Champion Dec 20, 2017

After you changed the position did it work?

In post functions the variable issue is passed automatically. You can get Issue key by calling 

issue.getKey()

In your post-function.

@Alexey Matveev

Hi Alexey,

Its running but taking the one month old date. Ideally it should be one month ahead of the creation date.

 

post function.png

@Alexey Matveev

Ideally as per the logic it should take one month ahead date but its  taking the one month old date

@Alexey Matveev

 

Is it due to server time mismatch ?

Alexey Matveev Community Champion Dec 20, 2017

Yes, my guess is that your server time is wrong

Hi Shrikant,

if your looking for SIL code.

Try this one:
%customfield_id%="created + "30d";

Thank you,
Randy

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

Meet the AUG leaders of Northern Virginia

@Rachel Wright (Jira Genie), @Billy Poggi (AUG NOVA, DC), and @Dana Jansen (Confluence Queen) are just some of the folks that lead one of the world's most active Atlassian User Group (AUG)....

157 views 5 9
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