Pre-populate but don't overwrite date field on transition

Trudy Claspill April 18, 2018

We are using JIRA Server 7.6.4 and we have Adaptavist ScriptRunner v5.3.9.

I am a developer, but I'm new to script writing in JIRA, and with Groovy.

(Side note - if you have recommendations for tutorials on this topic, I would welcome the pointers.)

I have a custom Date Picker field - Started. When an issue transitions to In Progress I want to

- check if the field already has a value

- if it doesn't, then populate it with today's date

- if it does already have a value, don't change the value

- regardless, still allow the user to edit it before completing the transition.

I think a Behavior is the right way to implement this. I've found multiple examples for prepopulating fields, but haven't been able to get them to work quite right. My stumbling block seems to be figuring out if the field already has a value.

Here is the code I have tried.

if (getActionName() != "In Progress") {
return // not the In Progress transition, so don't set default value}

def cField = getFieldByName("Started")
def cValue = cField.getValue()


if ( cValue ) {
return // Actual Start date set previously. Don't reset}

def newValue = new Date().format("d/MMM/yy")
cField.setFormValue(newValue)

 With this code, the field is always set to the current date, even if it has a value.

Thanks in advance.

 

1 answer

1 accepted

0 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 18, 2018

Kindly run the following script and have a look in the logs what is the value of the Started field when the field is empty:

if (getActionName() != "In Progress") {
return // not the In Progress transition, so don't set default value}

def cField = getFieldByName("Started")
def cValue = cField.getValue()

log.error("cValue: " + cValue)
if ( cValue ) {
return // Actual Start date set previously. Don't reset}

def newValue = new Date().format("d/MMM/yy")
cField.setFormValue(newValue)
Trudy Claspill April 19, 2018

Thank you for the suggestion, Alexey.

Where will the log.error message be written?

Note I am working with an inline script in JIRA, not with an IDE.

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 19, 2018

Errors will be in the atlassian-jira.log. You can find more info here:

https://confluence.atlassian.com/jira063/where-are-the-application-server-logs-683542381.html

Trudy Claspill April 23, 2018

In the log this message is getting written:

cValue: null

 

The same message is written when the field is empty and when it is already populated with data.

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 25, 2018

I guess, you script works on the initialization of your behaviour. Add the Date Picker field - Started to the behaviour and add this script to the field.

Trudy Claspill May 9, 2018

Hello Alexey,

Based on your comment I finally realized my misunderstanding about the Behavior functionality, and the difference between having a Field Validator script vs. an Initialiser function script.

I moved the script to be a field validator script and it worked as intended.

Thank you for your help!

Suggest an answer

Log in or Sign up to answer