Scriptrunner listener to update custom date/time field

Conor McGmail February 7, 2017

Hi All,

I need to write a listener that will add current timestamp to a custom date/time fields when another field is updated.

For example, if custom checkbox "Approved" is set, the listener detects this change and will timestamp the custom date/time field "Approved Date".

I had intended to simply do this through the JIRA Suites Utility plugin on a workflow transition, but the requirements have changed and this needs to be done on an "Edit" and hence needs to be detected and updated by a custom listener.

 Can @Thanos Batagiannis [Adaptavist] , @Jamie Echlin [Adaptavist] or some of the other Adaptavist guys please help me out here?

Any help is much appreciated.

Regards,

Conor

 

1 answer

1 accepted

9 votes
Answer accepted
Thanos Batagiannis _Adaptavist_
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.
February 7, 2017

Hi Conor,

Try something like 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import java.sql.Timestamp

Issue issue = issue
def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find {it.field == "CheckBox A"}
if (change) {
    def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Approved DateTime")
    def changeHolder = new DefaultIssueChangeHolder()
    def now = new Timestamp(new Date().getTime())
    cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), now),changeHolder)
}

Please let me know if this does the trick.

regards, Thanos

Conor McGmail February 15, 2017

Hi @Thanos Batagiannis [Adaptavist], thank you for that great assistance. I was about to accept the answer smile until I hit a real roadblock - the listener isn't firing when I try to bulk transition issues!

There will be occasions when I want to bulk edit closed issues to update these fields, restricting it to the Product Owner role.

To do this I intended to add a transition to loop from Closed to Closed and call it "Product Owner Update". This would present the screen to allow for the fields to be updated. All of this works when working on a single issue, but when I try to bulk transition (update) a set of Closed issues the transition does not fire sad 

 

 

 

 

Thanos Batagiannis _Adaptavist_
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.
February 15, 2017

Hi Conor, 

How do you bulk update the issues ?

Conor McGmail February 15, 2017

I'm trying to bulk update Closed issues in this scenario, so I'm actually bulk transitioning (Closed to Closed) via a transition that is restricted to a limited set of users

 

image2017-2-15 15:10:19.png

 

The transition fires the "Issue Updated" event which the listener picks up. Works on single issues, but doesn't fire or isn't detected when doing bulk transitions

Conor McGmail February 15, 2017

these status properties are my saviour, and I can now edit closed (bulk edit even) and do not need to do it via bulk transitions

 

image2017-2-15 17:0:24.png

 

 

Bunny August 22, 2017

Can I perform the operation with a text field instead of a Checkbox?

Can any one guide me to any resources or the code?

any help is appreciated.

Suggest an answer

Log in or Sign up to answer