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.
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
Is 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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Thanos Batagiannis [Adaptavist] thanks for the code
how do we get Display name instead of username?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Thanos Batagiannis [Adaptavist] Thank you for sharing your knowledge, I have question
I get a user key instead of username
Field Indicator updated from user: JIRAUSER13900 from null to Other
In API it says getUsername()
Deprecated. How can I get user full name or username? Any suggestion?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Any suggestions on getting user name instead of getting user key?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey Tukaram,
Can you please make sure that you did not forget to add the safe navigation operator (?) before the last() ?
regards, Thanos
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Thanos Batagiannis [Adaptavist]
Hi Thanos,
Thank you for providing a good script really appreciate your support but I have a different problem that i need the name of user who update the issue last time and it should include the comment section as well like through your script i am geting the name of user who are updating any field in my issue but it is not working when someone comment on the issue could you please help me with this?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.