how to pull updated date field data in Script runner

A November 12, 2015

i am trying to pull updated date field value if my issue status is closed.

want to show in the scripted field.

Below code only gives the diff from created date that also not in number.

import com.atlassian.core.util.DateUtils
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.history.ChangeItemBean

def componentManager = ComponentManager.getInstance()
def changeHistoryManager = componentManager.getChangeHistoryManager()

def inProgressName = "In Progress"

def rt = [0]
changeHistoryManager.getChangeItemsForField (issue, "status").reverse().each {ChangeItemBean item ->

    def timeDiff = System.currentTimeMillis() - item.created.getTime()
    if (item.fromString == inProgressName) {
        rt << -timeDiff
    }
    if (item.toString == inProgressName){
        rt << timeDiff
    }
}

// NOTE: doesn't show anything if less than 60 seconds
DateUtils.getDurationString(Math.round(rt.sum() / 1000))

2 answers

1 accepted

4 votes
Answer accepted
Jeff Louwerse
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.
November 12, 2015

If you just want the date an issue was closed

item.created gives you the timestamp of when that history entry.

You also are not checking what that actual status so it loops though each status until it gets to the first history entry.   To get the most recent CLOSED status..

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("ZAA-2");
ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager();

def X = changeHistoryManager.getChangeItemsForField (issue, "status")
for (item in X?.reverse())
{   if(item.getToString() == 'Closed')
    {log.error("X = ${item.getToString()} ..  ${item.created} ${item.created.getDateString()}"); break;}
}

 

logs  X = Closed   2014-08-29 08:21:59.0 .. 8/29/14

 

For here you can do want ever calculation you want if you were looking to calculate say "issue has been closed for XXX days"

 

A November 12, 2015

Thanks.. i did not understand the part of ZAA-2 as issue object. it should be dynamic. and is it possible to return the values to another custom field

A November 12, 2015

@Thanos Batagiannis [Adaptavist]

A November 12, 2015

any suggestions???

A November 12, 2015

Never mind got the answer replace "ZAA-2" with issue.id

Jeff Louwerse
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.
November 13, 2015

ya sorry, I use that in scriptrunner to pull one of my test issues so that I can test a piece of code to make sure I am not writing junk code in an answer.

JamieA
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.
November 15, 2015

Note that you should use issue.getCreated(), not issue.created. Because it clashes with issue.isCreated(), which is taken in preference when you use the property access format.

A November 15, 2015

currently i am getting the out put in Mon Nov 23 03:01:13 CST 2015 , requirement Nov 23 2015

JamieA
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.
November 16, 2015

add: .format("MMM dd yyyy") or whatever

A November 17, 2015

Jamie Scripted field is a Date time picker. Due to this it is returning value as Invalid date

0 votes
A November 17, 2015
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
 
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("ZAA-2");
ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager();
 
def X = changeHistoryManager.getChangeItemsForField (issue, "status")
for (item in X?.reverse())
{   if(item.getToString() == 'Closed')
    {log.error("X = ${item.getToString()} ..  ${item.created} ${item.created.getDateString().format("MMM dd yyyy")}"); break;}
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events