Need to get first date of the status for Scripted Field.

rajeswari saravanan June 26, 2023

For eg -

When status is moved from open to In progress,should get the first date when its moved to In progress using a Scripted field

1 answer

1 accepted

1 vote
Answer accepted
Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 26, 2023

Hi, @rajeswari saravanan 

Try this:

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

import java.sql.Timestamp

ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

String fieldName = "Status"
String statusName = "In Progress"

Issue issue = issue as Issue

List<ChangeHistory> allChanges = changeHistoryManager.getChangeHistories(issue)
List<ChangeHistory> cfChanges = []
Timestamp firstChange = null

allChanges.each { ChangeHistory change ->
if (fieldName.toLowerCase() == change.getChangeItemBeans().field[0].toLowerCase()) {
cfChanges.add(change)
}
}

if (cfChanges) {
// Additional sorting by date
cfChanges = cfChanges.sort {
it.getTimePerformed()
}

firstChange = cfChanges.findAll {
it.getChangeItemBeans().toString.first() == statusName
}.first().timePerformed
}

return firstChange
rajeswari saravanan June 26, 2023

Thanks for your help @Evgenii .Its working as expected 👍🏻

Suggest an answer

Log in or Sign up to answer