how to pull updated date field data in Script runner

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

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"

 

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

@Thanos Batagiannis [Adaptavist]

any suggestions???

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

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.

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.

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

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

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

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 Join to answer
Community showcase
Emilee Spencer
Published Friday in Marketplace Apps

Marketplace Spotlight: DeepAffects

Hello Atlassian Community! My name is Emilee, and I’m a Product Marketing Manager for the Marketplace team. Starting with this post, I'm kicking off a monthly series of Spotlights to highlight Ma...

51 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot