How to get previous changed custom field data.

HIX JIRA June 25, 2018

Hi All,

I have custom field X of type multi-text. I'm creating another scripted field Y which displays the X previous data using script runner.

Please let me know any sample script to achieve this.

 

TIA!

 

 

4 answers

1 accepted

2 votes
Answer accepted
Alexey Matveev
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.
June 25, 2018

Hello,

You can find an answer here:

https://community.atlassian.com/t5/Jira-questions/How-to-get-previous-and-current-values-of-all-the-changed-fields/qaq-p/269702

The base script is like this:

def changeHistoryManager = com.atlassian.jira.component.ComponentAccessor.getChangeHistoryManager()
def created = changeHistoryManager.getChangeItemsForField(issue, "your custom field name")

HIX JIRA June 26, 2018

Thank you @Alexey Matveev

Here is my code 

import com.atlassian.jira.component.ComponentAccessor
def changeHistoryManager = com.atlassian.jira.component.ComponentAccessor.getChangeHistoryManager()
def old = changeHistoryManager.getChangeItemsForField(issue, 'Original Requirement')
return old.fromString

This code prints all the previous data used in that field in a square brackets

Example : [text]

if the custom field is null then it print empty square bracktes [].

My Question is,

1. How to get only last changed value in the output instead of getting old data from the beginning.

2. How do I eliminate these brackets in output?

 

TIA!  

Alexey Matveev
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.
June 27, 2018

If you get brackets in groovy it means that the class of the old variable is the List. You need to get an element from the list. If you want to get the last element you should write:

old?.last().fromString

If you want to get the first element then:

old?.first().fromString

Like # people like this
Alok Kumar May 13, 2022

Thanks Alexey....

This is really helpful for me as well.

Anar Mahmudzada June 15, 2022

Thank you very much, Alexey. You deserve all the best compliments.

0 votes
Alex
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.
January 29, 2024

try this :

 

import com.atlassian.jira.component.ComponentAccessor

def issue = Issues.getByKey('PROJ-XXXX')

final String customFieldName = "your_custom_field"
def changeHistoryManager = ComponentAccessor.changeHistoryManager
def changeHistory = changeHistoryManager.getChangeItemsForField(issue, customFieldName)

changeHistory.last().fromString
0 votes
Inetinfo Inetinfo November 15, 2023

ank you Alexey Matveev

0 votes
SMAtlassianMber April 20, 2020

Hello @Alexey Matveev  - This is amazing. 

Is there anyway to extract the date as well as the field values?

Also, if we wanted each change separated for easier viewing, something better than just the comma, can that be done as well? 

Thanks in advance for any help.

SMK

Vijay Sv January 18, 2021

@SMAtlassianMber Did you get any workaround to separate the change based on Date or with any sort by order?

Suggest an answer

Log in or Sign up to answer