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

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

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

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

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

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)

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

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

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

314 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