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

Shrikant Maheshwari December 19, 2017

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
Randee024 January 10, 2018

Hi Shrikant,

if your looking for SIL code.

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

Thank you,
Randy

Tom October 28, 2019

Hi Randy,

Where is this meant to go?

Is this for a script runner post-function?

I've tried this but it fails at '%'

Randee024 October 28, 2019

Try to read on SIL documentation you can use this plug in on your back end part of jira.

https://confluence.cprime.io/display/SIL/Variable+resolution

1 vote
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, 2017

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()))
Shrikant Maheshwari December 20, 2017

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

Like Alma Morales likes this
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, 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.

Shrikant Maheshwari December 20, 2017

@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
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, 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. 

Shrikant Maheshwari December 20, 2017

@Alexey Matveev

The circle is neither turning green nor red

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, 2017

Try to use a different browser

Shrikant Maheshwari December 20, 2017

@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
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, 2017

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

Shrikant Maheshwari December 20, 2017

@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
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, 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.

Shrikant Maheshwari December 20, 2017

@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
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, 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.

Shrikant Maheshwari December 20, 2017

@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

Shrikant Maheshwari December 20, 2017

@Alexey Matveev

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

Shrikant Maheshwari December 20, 2017

@Alexey Matveev

 

Is it due to server time mismatch ?

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, 2017

Yes, my guess is that your server time is wrong

Suggest an answer

Log in or Sign up to answer