Adaptavist Scriptrunner - How to record date/time into an empty field during a transition?

licence
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 30, 2019

Hello,

I am creating a post function that would record the date/time of a transition into the "Dev Done Test" field (if it's empty). I am trying to write a script that would do exactly that, but with no luck.

-------------------------------------------------------

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def cf = customFieldManager.getCustomFieldObjects(issue).find { it.name == "Dev Done Test" }
def cfValues = issue.getCustomFieldValue(cf)
def dateCf = customFieldManager.getCustomFieldObject("customfield_13300") // Date time fields require a Timestamp

if (cfValues['dateCf'] == null) {
issue.setCustomFieldValue(dateCf, new Timestamp((new Date()).time))
}

-------------------------------------------------------

Could you tell me what am I doing wrong, please?

2 answers

0 votes
fran garcia gomera
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.
September 30, 2019

You can try something like this:

import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import java.sql.Timestamp;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue

def issueManager = ComponentAccessor.getIssueManager()

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def changeHolder = new DefaultIssueChangeHolder()


def cf = customFieldManager.getCustomFieldObjects(issue).find { it.name == "Dev Done Test" }

def today = new Timestamp(new Date().time)
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), today),changeHolder)
Kamil Beer October 7, 2019

Hello Fran,

thank you - as stated above, could you please tell me what should I add to make the script conditional? Ex. When the field is empty, to not modify it?

The use case is that we want this field filled during one transition OR at another one (but if the second, later one, happens, we don't want the contents of the field to be overwritten).

fran garcia gomera
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.
October 8, 2019

This should work:

if (issue.getCustomFieldValue(cf)==null){
    def today = new Timestamp(new Date().time)
    cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf),today),changeHolder)
}
0 votes
Ravi Sagar _Sparxsys_
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.
September 30, 2019

Hi @licence 

Why do you want to save transition date in a custom field? You can fetch it from the change history.

Start here: https://scriptrunner.adaptavist.com/latest/jira/recipes/scriptfields/dateOfFirstTransition.html

You can modify the code to look for the transition of your choice.

Ravi

Kamil Beer October 7, 2019

Hello Ravi,

our stakeholders store the information when the issue was in "Dev Done" status in a report of tens of issues, so it's a bit harder to browse issue by issue.

Thanks for the link. Could you please tell me what should I add to make the script conditional? Ex. When the field is empty, to not modify it?

The use case is that we want this field filled during one transition OR at another one (but if the second, later one, happens, we don't want the contents of the field to be overwritten).

Suggest an answer

Log in or Sign up to answer