Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,412,022
Community Members
 
Community Events
169
Community Groups

Script listener for changes to custom field runs too often

Hi,

 

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')

changeItems.any{
    if(it.field == 'SomeDate')
    	commentManager.create(issue,user,comment,true)
}

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!

 

Thanks,

Tobias

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
}

regards,

Thanos

Hi Thanos,

 

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

 

Thanks,

Tobias

Hi,

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, event.issue.id, issueInputParameters)

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

Thanks,

Nhung

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. 

 

Thanks,

Rajesh

TAGS

Atlassian Community Events