JIRA & Groovy - Update Date Time of custom field based on comparison between two others.

Camila Castillo June 2, 2017

Hello,

Please forgive my ignorance in advance, I'm completely new to JIRA and Groovy expressions, having come from a front-end and PHP background... would really appreciate any assistance or pointers.

 

Goal: 

Create a POST-function that will automatically set the time and date of a custom field "Quote Status Updated On (Date Time Picker)", based on whether or not the custom field "Quote Status (Select List (Single choice) )" has changed.

Attempt:

I tried creating a new field "Previous Quote Status (Select List (Single choice) )" that will copy the last value of "Quote Status", with the intention of comparing the two, and updating "Quote Status Updated On" with the current Date Time if there has been a change.

 

1.  Copy Value From Field to Field (JMWE add-on)

Capture.PNG

 

Unfortunately the above does not successfully copy the value as needed, giving the following error on the view issue page:

 

Capture.PNG

 

2. Set Field Value to constant or Groovy expression (JMWE add-on) Function for this transition

 

Capture.PNG

The above obviously does not work since step 1 is does not copy the value, but I wonder if the syntax is correct?

 

If anybody has any resources, like a list of commands or expressions that are useful it would be a lifesaver, as the documentation I have does not seem comprehensive.

Thank you in advance!!

1 answer

0 votes
Thomas Venekamp June 6, 2017

Hello,

you can achieve your goal with the following code, that you need to add as a post-function.

I assume, that the field "Quote Status" can be changed during the transition.

 

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

// Manager
def cfm = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()

def issueBefTrans = issueManager.getIssueObject(issue.getKey())
if (issue.getCustomFieldValue(cfm.getCustomFieldObject("customfield_15007")) != issueBefTrans.getCustomFieldValue(cfm.getCustomFieldObject("customfield_15007"))) {
 cf_quoteStatusUpdatedOn = cfm.getCustomFieldObject("customfield_15008")
 cf_quoteStatusUpdatedOn.getCustomFieldType().updateValue(cf_quoteStatusUpdatedOn, issue, new Timestamp(new Date().getTime()))
}

Don't forget to adjust the customfield IDs to your environment.
'Quote Status' has the ID 15007
and 'Quote Status Updated On' has the ID 15008 in my environment.

 

This post-function has to be executed first during your transition, because the variable 'issue' holds the current fields incl. the values in screen. Whereas the variable 'issueBefTrans' holds all values before the transition. But this is only true, if the script will be executed first before the actual transition will be performed.

I don't know what documentation you use, but these sources offer everything you need:
https://scriptrunner.adaptavist.com/latest/jira/quickstart.html
and the official API:
https://docs.atlassian.com/jira/7.3.1/ (Our current JIRA version is 7.3.1)
Here is the list to all API versions:
https://docs.atlassian.com/jira/

Suggest an answer

Log in or Sign up to answer