Script runner to pull user details for last field data change? Edited

Hi All,

I have been trying to figure out how I can use Script Runner on a Jira Server 6.3 to show a calculated value which is based on a field that has been edited by a user.

Basically I have one field that a user can edit, and by all accounts, if a user edits that field, the change is record along with the details of the user who edited it (as you can see this in the history of the jira issue).

So, I want to pull that data for that last cange of that field and display the user name in a Groovy Scripted Field.

Is this even possible?  I am ssuming I would need to run some sort of Query in my script which will return a user object type, or even just some text, and then use that result to display in the custom Scripted FIeld.

2 answers

1 accepted

0 votes
Accepted answer

Hi Tristan,

You can create a Scripted Field and it's script will be 

import com.atlassian.jira.component.ComponentAccessor

def changedItem = ComponentAccessor.getChangeHistoryManager().getAllChangeItems(issue)?.last()
"Field ${changedItem?.field} updated from user ${changedItem?.getUserKey()}"

And then in your issue view screen will get something like 

Screen Shot 2017-05-29 at 17.32.36.pngIs this what you were looking for ?

Hi Thanos,

Almost.  That gives the last changed field and the user linked with the change.  I am looking for a specific field.

Ive looked through the API and a couple of examples, and coined something together, but I don't think its the best way to do this, nor do I think it works.  How do you go about adding logging to these scripts?

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


ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

String fieldName = 'My Custom Field'
String lastUser = 'None'

List<ChangeHistory> changeHistory

changeHistory = changeHistoryManager.getChangeHistories(issue)
changeHistory.sort{it.getTimePerformed()}

for(ChangeHistory change: changeHistory){
    for(ChangeItemBean changeItemBean: change.getChangeItemBeans()) {
        if (changeItemBean.getField().equals(fieldName)){
          lastUser = change.getAuthorDisplayName()
        }
    }    
}

return lastUser

Thanks

Hi Tristan,

Fair enough, then you will need 

import com.atlassian.jira.component.ComponentAccessor
import static com.atlassian.jira.issue.IssueFieldConstants.*

def changedItem = ComponentAccessor.getChangeHistoryManager().getAllChangeItems(issue)?.findAll {it.field == PRIORITY}?.last()
"Field ${changedItem?.field} updated from user ${changedItem?.getUserKey()} from ${changedItem?.from} to ${changedItem?.to}"

Regarding your question about logging, have a  look at https://www.adaptavist.com/doco/display/SFJ/Set+logging+to+help+debug+your+scripts 

Brilliant!  Thanks, that is a much simpler bit of scripting.

@Thanos Batagiannis [Adaptavist] thanks for the code 

how do we get Display name instead of username?

Please help me , how to stop logging below message to jira  LOG FILE for above script ?

Script field failed on issue: SSD-1, field: My scripted field
java.util.NoSuchElementException: Cannot access last() element from an empty List
at Script50.run(Script50.groovy:6)

Hey Tukaram,

Can you please make sure that you did not forget to add the safe navigation operator (?) before the last() ?

regards, Thanos

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Thursday in Jira

Continuous visibility with the new Jira Software Cloud and Bitbucket Cloud integration

Hey Atlassian community, I am a PM on the Bitbucket Cloud team and I am excited to share that we just released an improvement to the Jira Software and Bitbucket Cloud integration. With the newly im...

82 views 0 4
Join discussion

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