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

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 [cPrime]

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

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 [cPrime]

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

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 [cPrime]

The circle is neither turning green nor red

Try to use a different browser

@Alexey Matveev [cPrime]

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)

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

@Alexey Matveev [cPrime]

 

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

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 [cPrime]

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

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 [cPrime]

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 [cPrime]

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

@Alexey Matveev [cPrime]

 

Is it due to server time mismatch ?

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

Hi Randy,

Where is this meant to go?

Is this for a script runner post-function?

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

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

334 views 1 3
Join discussion

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