Set current date in custom field during transition using script post function (ScriptRunner)

Lauren Robinette December 20, 2018

Hi There! I'm brand new to Groovy Scripts but very familiar with Jira Server.

I'm using Jira Server v7.10.1. I am trying to implement a script post-function using ScriptRunner which will update my custom field for Time Stamp In Progress with the date and time that the transition from Create to In Progress occurs.

I've been scouring the Atlassian Community posts, and am currently testing out the script below. We are using Jira Business, not Software, so we do not have Component Manager declared. This may not be a concern, but wanted to call it out! 

The field I want to populate with the current date and time is Time Stamp In Progress (field ID 16882).

Any guidance would be very appreciated :) Thank you!

 

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("Time Stamp In Progress") )


// Getting custom field object
cfoFuncDate = customFieldManager.getCustomFieldObjectByName("Time Stamp In Progress")


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


// set value
if (!cfFuncDate)
issue.setCustomFieldValue(cfoFuncDate, today)

2 answers

1 accepted

3 votes
Answer accepted
Lauren Robinette December 21, 2018

Hi @Tom Lister

Without JSU - Suite Utilities for Jira, updating a custom issue field is not an option for a post-function. I do have ScriptRunner and determined how to write the Groovy Script.

  1. Put the Timestamp field (date and time or just date) on the corresponding screen of the issue(s) impacted by the transition.
    1. I put Timestamp in Progress on my screen.
  2. Navigate to the Transition in your workflow. Create a post-function - select Script Post-Function [ScriptRunner].
  3. Select custom script post-function.
  4. Input the following script:

import com.atlassian.jira.component.ComponentAccessor

import java.sql.Timestamp

 

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def dateCf = customFieldManager.getCustomFieldObject("customfield_16920") // Date time fields require a Timestamp

issue.setCustomFieldValue(dateCf, new Timestamp((new Date()).time))

(Update the bold 16920 with the timestamp's field ID - you can find the field ID on the Custom fields page by clicking the gear and hovering over Configure. Check the URL populating at the bottom of the screen for the field ID. It takes a second to populate.)

5. Save your post-function.

S KHADHAR MOHAIDEEN October 6, 2020

Great, thanks. This was really useful to update a date-time field when it is empty.

3 votes
Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 21, 2018

Hi @Lauren Robinette

you should be able to do this by using a post function to set custom field value and using %%CURRENT_DATETIME%%

See similar post here 

https://community.atlassian.com/t5/Jira-questions/How-do-I-set-a-custom-field-with-the-current-date-time-in-a-post/qaq-p/143415

Richard Bone July 29, 2019

Hi @Tom Lister & @Lauren Robinette I think the suggestion you make is valid and works ... but only for new issuetypes that use this transition. It doesn't address how to update/fix previous values 

Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 30, 2019

Hi @Richard Bone @Lauren Robinette 

I think the only way to address that is to run a groovy script over the issue history. Below is an example of a scripted field I wrote to retrospectively find the date of the first transition. This is an example of the logic you would have to work into an update script.

package com.onresolve.jira.groovy.test.scriptfields.scripts

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
List<ChangeItemBean> created = changeHistoryManager.getChangeItemsForField(issue, "status").sort { it.getCreated() }
ChangeItemBean change
def createdTime
if (created)
{
change = created.first()
createdTime = change.getCreated()
}//.first()

createdTime ? new Date(createdTime.getTime()) : null

Suggest an answer

Log in or Sign up to answer