Forums

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

Scripted post function to set a date value in custom field

Alex Christensen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 12, 2015

I want to set a custom date field using a post function, but I only want to capture the current date if the field has not yet been set. I'd use a regular post function to do this, but once this field is populated, I don't want it to get overwritten, which is why I've turned to a script for this. I'm very new to groovy scripting and don't have much of a coding background - here's my attempt after looking at a lot of resources here on Answers and elsewhere.

import java.util.*
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.CustomFieldManager
import com.atlassian.jira.issue.Issue

def customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
def date = new Date()
Issue issue = issue
def funcDate = issue.getCustomFieldValue( customFieldManager.getCustomFieldObjectByName("Ready for Func Test") ) 

if (!funcDate) // if this field is empty, then the current date is set for this field
	issue.setCustomFieldValue("Ready for Func Test", date)

Basically, we want to be able to report on the date in which the issue was first transitioned to a particular status. I don't want to overwrite the value if the same transition is used again (which is very possible in our workflow setup). I've tried several variations of the code above, but I can't seem to get it to work the way I want it to. Even if the field is empty, the field does not get a value set to it during the transition for which I've configured this post function.

Is what I'm trying to do possible via a scripted post function? Am I at least close? smile Thanks for your help in advance!

2 answers

1 accepted

4 votes
Answer accepted
Alex Christensen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 14, 2015

Found my answer using the answer in from a previous question here. Here's the final script I used:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import static java.lang.Math.*
import java.sql.Timestamp

// get current issue
Issue issue = issue
customFieldManager = ComponentManager.getInstance().getCustomFieldManager()


// Getting custom field value
cfFuncDate = issue.getCustomFieldValue( customFieldManager.getCustomFieldObjectByName("Ready for Func Test") )


// Getting custom field object
cfoFuncDate = customFieldManager.getCustomFieldObjectByName("Ready for Func Test")


// get today's date
today = new java.sql.Timestamp(new Date().getTime())


// set value
if (!cfFuncDate)
	issue.setCustomFieldValue(cfoFuncDate, today)
Priyanka Khare
Contributor
August 23, 2018

hello 
@Alex Christensen will this run in JIRA cloud too?

Alex Christensen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 23, 2018

I don't believe that it will. I don't have a Cloud instance to test this, but I don't think ScriptRunner on Jira Cloud allows you to configure scripted workflow post-functions.

Dave Furlani February 17, 2019

The above code gives me errors in the scripting window as soon as I paste it in - non declared variable, missing method and some other static review errors. I'm sure it is close but I'm very new to that language and the libraries/methods available.

Is there a working version of code to put today's date into a date field if it is empty?

Alex Christensen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 19, 2019 edited

@Dave Furlani this should probably work for you and stop throwing those non-declared variable errors.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import java.sql.Timestamp

def customFieldManager = ComponentAccessor.getCustomFieldManager()

// Getting custom field object
def cfoFuncDate = customFieldManager.getCustomFieldObjectByName("Ready for Func Test")

// Getting custom field value
def cfFuncDate = issue.getCustomFieldValue(cfoFuncDate)

// get today's date
def today = new java.sql.Timestamp(new Date().getTime())

// set value
if (!cfFuncDate) {
issue.setCustomFieldValue(cfoFuncDate, today)
Dave Furlani February 19, 2019 edited

Thanks, but I get a static check error on line 1 saying unable to resolve class com.atlassian.jira.ComponentAccessor.

The script could not be compiled: <pre>org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script86.groovy: 1: unable to resolve class com.atlassian.jira.ComponentAccessor @ line 1, column 1. import com.atlassian.jira.ComponentAccessor ^ 1 error </pre>.

That's the only error though, which is an improvement on the script in the earlier posts.

So close.

Like Josef Zahlauer likes this
Alex Christensen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 20, 2019

Oops, my bad. I fixed the script in my last post and it should no longer throw that error. :)

Dave Furlani February 21, 2019

Thanks Alex, that version worked perfectly.

You are a champ.

Alin Faur
Contributor
February 25, 2021

Thanks a lot for sharing this script! It was exactly what I was looking for and it worked on the first try!

0 votes
JamieA
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.
May 12, 2015

Slight alteration:

def cfFuncDate = customFieldManager.getCustomFieldObjectByName("Ready for Func Test")
def funcDate = issue.getCustomFieldValue(cfFuncDate)
if (!funcDate) // if this field is empty, then the current date is set for this field
    issue.setCustomFieldValue(cfFuncDate, date)

 

 

Alex Christensen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 12, 2015

Hi, Jamie - thanks for your help! Unfortunately, I made this alteration and the field still isn't populating. I double checked the name of the custom field to make sure I wasn't going crazy, too. :) Is there anything I need to do with the date format? JIRA presents the date as DD/MM/YY - if that's an issue from how Date() pulls the current date, how can I alter my code to populate the date in this format? Also, just to test, I took out my if statement to see if anything would write to that custom field anyway - I got the same result as before. The field is still empty.

JamieA
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.
May 12, 2015

Where is the post-function in the list of post-functions? If towards the top, it should work. Can you find your atlassian-jira.log and see if there are any errors...

DevaKiran
Contributor
March 15, 2019

Hi @JamieA ,

I have a Date Picker Field. I need a script to validate the Date which is <=now. If date is greater throw an exception

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.

Register Now
AUG Leaders

Atlassian Community Events