Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Script listener for changes to custom field runs too often



I'm trying to write a custom listener that creates a comment every time a custom field is changed from one value to another (see code below).


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.model.ChangeGroup
import com.atlassian.jira.model.ChangeItem
import com.atlassian.jira.issue.history.ChangeItemBean

def commentManager = ComponentAccessor.getCommentManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def issue = event.issue
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def cf = customFieldManager.getCustomFieldObjectByName("SomeDate")
def sd= cf.getValue(issue) as Date
def someDate = sd.format("yyyy-MM-dd")

String comment = "*Automatic comment:* SomeDate was updated to " + someDate

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def changeItems = changeHistoryManager.getChangeItemsForField(issue,'SomeDate')

    if(it.field == 'SomeDate')

I believe that I've almost gotten it to work, the only problem is that comments are created even when another custom field is updated. I'm quite certain that the problem lies in the last rows of code since I'm not entirely sure how to use the changeItems variable. I tried using the changeItems.any code without defining changeItems as I've seen some examples of, but that gave errors saying that the variable was undeclared.


I currently have the listener to fire on Issue Updated events.


Any help would be greatly appreciated!




3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted

Hi Tobias

Try something like (what you missed is that in a listener you can get the changes via the event)

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Single User Picker CF"}

if (change) {
    log.debug "Value changed from ${change.oldstring} to ${change.newstring}"
	// your actions if the field has changed



Hi Thanos,


Thanks for your quick reply, I tried that and it works! Thanks a lot!





I created listener with below code having using event?.getChangeLog(), but it didnot work when change value of custom field 1, custom field2 can not be updated. Any help would be greatly appreciated!

ef customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueService = ComponentAccessor.getIssueService()

def customfield1_Changed = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "KBL_Van ban TC"}

def customField1 = customFieldManager.getCustomFieldObject("customfield_12709")
def customField2 = customFieldManager.getCustomFieldObject("customfield_13007")

def customField1Value = customField1.getValue(event.issue).toString()
def customField2Value = customField2.getValue(event.issue).toString()

if (customfield1_Changed) {
def issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.addCustomFieldValue(customField2.idAsLong, customField1Value)
def validationResult = issueService.validateUpdate(event.user,, issueInputParameters)

if (validationResult.isValid()) {
issueService.update(event.user, validationResult)
} else {
log.warn "Did not update issue: $validationResult.errorCollection.errors"



Hi Thonas,


I am looking similar custom listener, when the "Assignment group" is changed comment is required from user, can you please help me with this. 





Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you